Home | Projects | Notes > C++ Programming > Sequence Container - std::array (C++11)
std::array (C++11)
std::array (C++11)Declared in the <array> header file.
Fixed size array of any type - the size must be known at compile time.
Direct element access - Element access happens in constant time. (i.e., O(1))
Provides access to the underlying raw array.
std::array is implemented behind the scenes by the STL as a wrapper class around a raw array.
We can get access to that array address if we need to and we can use it with libraries that use raw pointers.
std::array differs from the raw array in that:
It's an object type.
It alwyas has its size associated with it.
It doesn't decay to a pointer to the first element of the array when passed into a function.
Use std::array for a fixed-size array instead of raw arrays unless you have a compelling reason to do so.
All iterators are available and do not become invalid since std::array is a fixed-size structure.
121// Initialization2std::array<int, 5> arr1{ {1, 2, 3, 4, 5} };3
4// Initialization5std::array<std::string, 3> arr2{6 std::string{"Kyungjae"},7 "Sunny", // C-style string will be converted to a std::string.8 std::string{"Yena"}9};10
11// Assignment12arr1 = {2, 4, 6, 8, 10};For more information, see cppreference.com.
121std::array<int, 5> arr1{1, 2, 3, 4, 5};2std::array<int, 5> arr2{10, 20, 30, 40, 50};3
4std::cout << arr1.size(); // 55std::cout << arr1.at(0); // 1 (Supports out-of-bounds check)6std::cout << arr1[1]; // 2 (Does not support out-of-bounds check)7std::cout << arr1.front(); // 1 (Returns reference to the first element)8std::cout << arr1.back(); // 5 (Returns reference to the last element)9std::cout << arr1.empty(); // 0 (false)10std::cout << arr1.max_size(); // 511arr1.swap(arr2); // Swaps the 2 arrays12int *p_data = arr1.data(); // Returns the raw array address
std::array1761234// accumulate()5
6// Display any array of integers of size 5 using range-based for loop7void print(const std::array<int, 5> &arr)8{9 std::cout << "[ ";10 for (auto const &n : arr)11 {12 std::cout << n << " ";13 }14 std::cout << "]" << std::endl;15}16
17// front(), back()18void test1(void)19{20 std::cout << "\nTEST1" << std::endl;21
22 std::array<int, 5> arr1{1, 2, 3, 4, 5}; // Double {{ }} if C++11.23 std::array<int, 5> arr2;24
25 print(arr1);26 print(arr2); // Elements are not initialized (contains garbage)27
28 arr2 = {10, 20, 30, 40, 50};29
30 print(arr1);31 print(arr2);32
33 std::cout << "Size of arr1 is: " << arr1.size() << std::endl; // 534 std::cout << "Size of arr2 is: " << arr2.size() << std::endl; // 535
36 arr1[0] = 1000; // Does not support out-of-bounds check.37 arr1.at(1) = 2000; // Supports out-of-bounds check.38 print(arr1);39 40 std::cout << "Front of arr2: " << arr2.front() << std::endl; // 1041 std::cout << "Back of arr2: " << arr2.back() << std::endl; // 5042}43
44// fill()45void test2(void)46{47 std::cout << "\nTEST2" << std::endl;48
49 std::array<int, 5> arr1{1, 2, 3, 4, 5}; // Double {{ }} if C++11.50 std::array<int, 5> arr2{10, 20, 30, 40, 50};51
52 print(arr1);53 print(arr2);54
55 arr1.fill(0);56
57 print(arr1);58 print(arr2);59
60 arr1.swap(arr2);61
62 print(arr1);63 print(arr2);64}65
66// data()67void test3(void)68{69 std::cout << "\nTEST3" << std::endl;70
71 std::array<int, 5> arr{1, 2, 3, 4, 5}; // Double {{ }} if C++11.72
73 int *ptr = arr.data(); // Address of the raw array within the object.74 std::cout << ptr << std::endl;75 *ptr = 10000;76
77 print(arr);78}79
80// sort()81void test4(void)82{83 std::cout << "\nTEST4" << std::endl;84
85 std::array<int, 5> arr{2, 1, 4, 5, 3}; // Double {{ }} if C++11.86 print(arr);87
88 std::sort(arr.begin(), arr.end());89 print(arr);90}91
92// min_element(), max_element()93void test5(void)94{95 std::cout << "\nTEST5" << std::endl;96
97 std::array<int, 5> arr{2, 1, 4, 5, 3}; // Double {{ }} if C++11.98
99 // Note: min_element() and max_element() returns 'iterators' to the elements100 // not the elements. 101 std::array<int, 5>::iterator min = std::min_element(arr.begin(), arr.end());102 auto max = std::max_element(arr.begin(), arr.end());103 std::cout << "min: " << *min << ", max: " << *max << std::endl;104}105
106// adjacent_find()107void test6(void)108{109 std::cout << "\nTEST6" << std::endl; 110
111 std::array<int, 5> arr{2, 1, 3, 3, 5}; // Double {{ }} if C++11.112
113 auto adj = std::adjacent_find(arr.begin(), arr.end());114 if (adj != arr.end())115 {116 std::cout << "Adjacent element found with value: " << *adj << std::endl;117 }118 else119 {120 std::cout << "No adjacent elements found." << std::endl;121 }122}123
124// accumulate()125void test7(void)126{127 std::cout << "\nTEST7" << std::endl; 128
129 std::array<int, 5> arr{1, 2, 3, 4, 5}; // Double {{ }} if C++11.130
131 // Note: Ensure that the data type of the first template parameter of the132 // array, 'sum', and the third argument to the accumulate() function are133 // all the same.134 int sum = std::accumulate(arr.begin(), arr.end(), 0);135 std::cout << "Sum of the elements in arr is: " << sum << std::endl;136}137
138// count()139void test8(void)140{141 std::cout << "\nTEST8" << std::endl; 142
143 std::array<int, 10> arr{1, 2, 3, 1, 2, 3, 3, 3, 3, 3};144 // Double {{ }} if C++11.145
146 int cnt = std::count(arr.begin(), arr.end(), 3);147 std::cout << "Found 3: " << cnt << " times." << std::endl;148}149
150// count_if()151void test9(void)152{153 std::cout << "\nTEST9" << std::endl; 154
155 std::array<int, 10> arr{1, 2, 3, 50, 60, 70, 80, 200, 300, 400};156 // Double {{ }} if C++11.157
158 // Find how many numbers are between 10 and 200 -> 50, 60, 70, 80.159 int cnt = std::count_if(arr.begin(), arr.end(), 160 [](int x) { return x > 10 && x < 200; });161 std::cout << "Found: " << cnt << " matches." << std::endl;162}163
164int main(int argc, char *argv[])165{166 test1();167 test2();168 test3();169 test4();170 test5();171 test6();172 test7();173 test8();174 test9();175 return 0;176}1211
2TEST13[ 1 2 3 4 5 ]4[ 0 0 1823312064 32761 8 ]5[ 1 2 3 4 5 ]6[ 10 20 30 40 50 ]7Size of arr1 is: 58Size of arr2 is: 59[ 1000 2000 3 4 5 ]10Front of arr2: 1011Back of arr2: 5012
13TEST214[ 1 2 3 4 5 ]15[ 10 20 30 40 50 ]16[ 0 0 0 0 0 ]17[ 10 20 30 40 50 ]18[ 10 20 30 40 50 ]19[ 0 0 0 0 0 ]20
21TEST3220x1fb35ffdf023[ 10000 2 3 4 5 ]24
25TEST426[ 2 1 4 5 3 ]27[ 1 2 3 4 5 ]28
29TEST530min: 1, max: 531
32TEST633Adjacent element found with value: 334
35TEST736Sum of the elements in arr is: 1537PS D:\workspace\sel> g++ .\array.cpp38PS D:\workspace\sel> .\a.exe 39
40TEST141[ 1 2 3 4 5 ]42[ 0 0 1823312064 32761 8 ]43[ 1 2 3 4 5 ]44[ 10 20 30 40 50 ]45Size of arr1 is: 546Size of arr2 is: 547[ 1000 2000 3 4 5 ]48Front of arr2: 1049Back of arr2: 5050
51TEST252[ 1 2 3 4 5 ]53[ 10 20 30 40 50 ]54[ 0 0 0 0 0 ]55[ 10 20 30 40 50 ]56[ 10 20 30 40 50 ]57[ 0 0 0 0 0 ]58
59TEST3600xd0b8dffc8061[ 10000 2 3 4 5 ]62
63TEST464[ 2 1 4 5 3 ]65[ 1 2 3 4 5 ]66
67TEST568min: 1, max: 569
70TEST671Adjacent element found with value: 372
73TEST774Sum of the elements in arr is: 1575
76TEST877Found 3: 6 times.78PS D:\workspace\sel> g++ .\array.cpp79PS D:\workspace\sel> .\a.exe 80
81TEST182[ 1 2 3 4 5 ]83[ 0 0 1823312064 32761 8 ]84[ 1 2 3 4 5 ]85[ 10 20 30 40 50 ]86Size of arr1 is: 587Size of arr2 is: 588[ 1000 2000 3 4 5 ]89Front of arr2: 1090Back of arr2: 5091
92TEST293[ 1 2 3 4 5 ]94[ 10 20 30 40 50 ]95[ 0 0 0 0 0 ]96[ 10 20 30 40 50 ]97[ 10 20 30 40 50 ]98[ 0 0 0 0 0 ]99
100TEST31010x8add5ffc10102[ 10000 2 3 4 5 ]103
104TEST4105[ 2 1 4 5 3 ]106[ 1 2 3 4 5 ]107
108TEST5109min: 1, max: 5110
111TEST6112Adjacent element found with value: 3113
114TEST7115Sum of the elements in arr is: 15116
117TEST8118Found 3: 6 times.119
120TEST9121Found: 4 matches.