The container array
Location
-
Courses
/
-
Complete C++ Course
/
-
The C++ Standard Library
/
-
Containers
/
The container array
Summary
array vs vector
The container std::array<T, N> stores its elements like a static array (In the static memory). Its only advantage over static arrays is only that it provides a few methods. Since its elements are stored in static memory, it has the same size limits as static arrays.
The first template argument of std::array<T, N> is the type of the elements and the second one is its size. Like the class std::vector, it has a method called size, which returns the size of the container. Note that it is declared in the header file <array>.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <array>
#include <vector>
int main()
{
std::array<int, 10> arr;
for(unsigned c = 0; c < arr.size(); c++)
arr[c] = c * c;
for(unsigned c = 0; c < arr.size(); c++)
std::cout << arr[c] << std::endl;
// Works fine.
std::vector<int> vec(10 * 1000 * 1000);
// Crashes: Size too big for static memory.
// std::array<int, (10 * 1000 * 1000)> arr2;
return 0;
}

Filling an array with a value
The method void std::array<T, N>::fill(const T & val) allows to set the value of all the elements of an array with the value given in argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <array>
int main()
{
std::array<int, 5> arr;
arr.fill(0);
for(unsigned c = 0; c < arr.size(); c++)
std::cout << arr[c] << std::endl;
std::cout << std::endl;
arr.fill(9);
for(unsigned c = 0; c < arr.size(); c++)
std::cout << arr[c] << std::endl;
return 0;
}

Swapping the content of two arrays
Using the method std::array<T, N>::swap(std::array<T, N> &arr), we can swap the content of two arrays of the same size.
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
#include <iostream>
#include <array>
int main()
{
std::array<int, 5> arr;
std::array<int, 5> arr2;
arr.fill(0);
for(unsigned c = 0; c < arr.size(); c++)
arr2[c] = c;
// Swaps the content of both arrays.
arr.swap(arr2);
// Prints the value of the elements of both arrays.
for(unsigned c = 0; c < arr.size(); c++)
std::cout << arr[c] << std::endl;
std::cout << std::endl;
for(unsigned c = 0; c < arr.size(); c++)
std::cout << arr2[c] << std::endl;
return 0;
}

Note that the container std::vector also has a method swap, but it does not require both vectors to have the same size.
Methods for accessing the elements
Like the class vector, the class array possesses the methods T & at(size_type index), to access the element at the index specified in argument, T & front(), to access the first element, and T & back(), the access the last element of the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <array>
int main()
{
std::array<int, 5> arr;
for(unsigned c = 0; c < arr.size(); c++)
arr[c] = c;
std::cout << arr.front() << " " << arr.back() << "\n\n";
for(unsigned c = 0; c < arr.size(); c++)
std::cout << arr.at(c) << ' ';
std::cout << "\n";
return 0;
}

Iterators
It is possible to use iterators with the class array and the methods returning iterators are the same as with the container vector.
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
#include <iostream>
#include <array>
int main()
{
std::array<int, 10> arr;
arr.fill(1);
std::array<int, 10>::iterator it = arr.begin()+1;
while(it != arr.end())
{
*it += *(it-1);
it++;
}
std::array<int, 10>::reverse_iterator revIt = arr.rbegin();
while( revIt != arr.rend())
{
std::cout << *revIt << ' ';
revIt++;
}
std::cout << "\n";
return 0;
}
