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// Initialization
2std::array<int, 5> arr1{ {1, 2, 3, 4, 5} };
3
4// Initialization
5std::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// Assignment
12arr1 = {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(); // 5
5std::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(); // 5
11arr1.swap(arr2); // Swaps the 2 arrays
12int *p_data = arr1.data(); // Returns the raw array address
std::array
1761
2
3
4// accumulate()
5
6// Display any array of integers of size 5 using range-based for loop
7void 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; // 5
34 std::cout << "Size of arr2 is: " << arr2.size() << std::endl; // 5
35
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; // 10
41 std::cout << "Back of arr2: " << arr2.back() << std::endl; // 50
42}
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 elements
100 // 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 else
119 {
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 the
132 // array, 'sum', and the third argument to the accumulate() function are
133 // 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
2TEST1
3[ 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: 5
8Size of arr2 is: 5
9[ 1000 2000 3 4 5 ]
10Front of arr2: 10
11Back of arr2: 50
12
13TEST2
14[ 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
21TEST3
220x1fb35ffdf0
23[ 10000 2 3 4 5 ]
24
25TEST4
26[ 2 1 4 5 3 ]
27[ 1 2 3 4 5 ]
28
29TEST5
30min: 1, max: 5
31
32TEST6
33Adjacent element found with value: 3
34
35TEST7
36Sum of the elements in arr is: 15
37PS D:\workspace\sel> g++ .\array.cpp
38PS D:\workspace\sel> .\a.exe
39
40TEST1
41[ 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: 5
46Size of arr2 is: 5
47[ 1000 2000 3 4 5 ]
48Front of arr2: 10
49Back of arr2: 50
50
51TEST2
52[ 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
59TEST3
600xd0b8dffc80
61[ 10000 2 3 4 5 ]
62
63TEST4
64[ 2 1 4 5 3 ]
65[ 1 2 3 4 5 ]
66
67TEST5
68min: 1, max: 5
69
70TEST6
71Adjacent element found with value: 3
72
73TEST7
74Sum of the elements in arr is: 15
75
76TEST8
77Found 3: 6 times.
78PS D:\workspace\sel> g++ .\array.cpp
79PS D:\workspace\sel> .\a.exe
80
81TEST1
82[ 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: 5
87Size of arr2 is: 5
88[ 1000 2000 3 4 5 ]
89Front of arr2: 10
90Back of arr2: 50
91
92TEST2
93[ 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
100TEST3
1010x8add5ffc10
102[ 10000 2 3 4 5 ]
103
104TEST4
105[ 2 1 4 5 3 ]
106[ 1 2 3 4 5 ]
107
108TEST5
109min: 1, max: 5
110
111TEST6
112Adjacent element found with value: 3
113
114TEST7
115Sum of the elements in arr is: 15
116
117TEST8
118Found 3: 6 times.
119
120TEST9
121Found: 4 matches.