Arrays
std::array
is a homogeneous container of fixed length. It needs the header <array>
. The std::array
combines the memory and runtime characteristic of a C array with the interface of std::vector
. This means in particular, the std::array
knows its size. You can use std::array
in the algorithms of the STL.
You have to keep a few special rules in your mind to initialise a std::array
.
std::array<int, 10> arr
- The 10 elements are not initialised.
std::array<int, 10> arr{}
- The 10 elements are default initialised.
std::array<int, 10> arr{1, 2, 3, 4, 5}
- The remaining elements are default initialised.
std::array
supports three types of index access.
The most often used first type form with angle brackets does not check the boundaries of the arr
. This is in opposition to arr.at(n)
. You will get eventually a std::range-error
exception. The last type shows the relationship of the std::array...