std::array#include <array> is a fixed-size array. It encapsulates a C-style array and stores its elements contiguously inside the object. The element type and size are given as template arguments and the size cannot change after declaration. It is available from the array header:
#include <array>
std::array#include <array><int, 3> arr = {1, 2, 3};Elements are accessed with [], and size() returns the number of elements:
arr[0]; // 1
arr[0] = 10;
arr.size(); // 3at() does the same as [] but throws an exception if the index is out of range instead of reading invalid memory:
arr.at(1); // 2
arr.at(5); // Throws std::out_of_range#include <stdexcept>.std::array#include <array> works with range-based for loops:
for (int x : arr) {
std::cout#include <iostream> << x << "\n";
}Note
Prefer std::array#include <array> over a C-style array of the same size. A C-style array does turns into a pointer when passed to a function, whereas std::array#include <array> keeps its size and is copied like any other object.
Dan