CKTIS / C++
std::array: a fixed-size array container
std::array is a contiguous container whose element count is known at compile time. It does not allocate a separate dynamic buffer.
Version: C++23
Syntax
template<class T, std::size_t N>
struct array {
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr size_type size() const noexcept;
};
std::array<T, N> values{elements...};std::array<T, N> from <array> wraps a built-in array with a container interface. It is an aggregate, stores elements contiguously, and makes N part of its type. It can be copied by value, compared, and passed to algorithms.
Aggregate initialization such as {1, 2} initializes remaining elements from an empty initializer list when N is larger. This yields zero for int and other arithmetic types; for a general T, its own initialization rules apply, including default member initializers of an aggregate. at() checks bounds and throws std::out_of_range; operator[] performs no check. fill assigns one value to every element.
std::array::begin returns an iterator to the first element, std::array::end returns the past-the-end iterator, and std::array::size always returns the constant count N; overloads for a const object return read-only iterators. For N == 0, the range is empty and begin() == end(). Calling front() or back() is then undefined. There are no size-changing insert or erase operations; use std::vector when size depends on runtime data.
Examples
#include <array>
#include <iostream>
int main() {
const std::array<int, 3> values{2, 3, 4};
std::cout << values.at(1) << ' ' << values.size() << '\n';
}
Result
3 3
std::array → 3 3