-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path02_array.cpp
More file actions
52 lines (42 loc) · 1.46 KB
/
Copy path02_array.cpp
File metadata and controls
52 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// std::array: compile-time fixed-size array demo
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
int main() {
// Value-initialized -> all zeros
std::array<uint8_t, 8> buf{};
buf[0] = 0xAA;
buf.at(1) = 0x55;
// Fill with a value using STL algorithm
std::fill(buf.begin(), buf.end(), 0xFF);
// Range-based for loop
for (auto b : buf) {
std::cout << static_cast<int>(b) << ' ';
}
std::cout << '\n';
// Compile-time lookup table with constexpr
constexpr auto make_crc_table = [] {
std::array<uint32_t, 256> t{};
for (size_t i = 0; i < 256; ++i) {
uint32_t crc = static_cast<uint32_t>(i);
for (int j = 0; j < 8; ++j) {
crc = (crc & 1) ? (0xEDB88320u ^ (crc >> 1)) : (crc >> 1);
}
t[i] = crc;
}
return t;
};
constexpr auto crc_table = make_crc_table();
static_assert(crc_table[0] == 0x00000000u);
static_assert(crc_table[255] == 0x2D02EF8Du);
std::cout << "CRC table[128] = 0x" << std::hex << crc_table[128] << '\n';
// Structured binding (C++17)
std::array<int, 3> a = {1, 2, 3};
auto [x, y, z] = a;
std::cout << std::dec << "Structured binding: " << x << ',' << y << ',' << z << '\n';
// .size() is constexpr, .data() gives raw pointer
std::cout << "sizeof(buf) = " << sizeof(buf) << '\n';
std::cout << "buf.size() = " << buf.size() << '\n';
return 0;
}