Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ std::array class

Reference for std::array: a fixed-size, stack-allocated array wrapper with iterators, bounds-checked at(), structured bindings, constexpr use and to_array.

std::array<T, N> is a thin wrapper around a built-in array whose size N is part of the type and known at compile time. It has no dynamic allocation: the elements live inside the object itself (on the stack when the array is a local variable). Unlike a raw array it does not decay to a pointer, can be copied, assigned, compared and returned from functions, and offers the standard container interface (begin, size, at, front, back, data, fill, swap).

It is an aggregate, so you initialize it with braces. It lives in <array>.

C++
#include <array>
std::array<int, 4> a = {1, 2, 3, 4};

Try it

C++ Basic use and copying

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ constexpr, structured bindings and to_array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template <class T, std::size_t N>
struct array;

// Access
constexpr T& operator[](size_type i);   constexpr T& at(size_type i);
constexpr T& front();   constexpr T& back();   constexpr T* data() noexcept;

// Capacity and operations
constexpr size_type size() const noexcept;   constexpr bool empty() const noexcept;
constexpr void fill(const T& value);         constexpr void swap(array& other);

// Helpers
template <class T, std::size_t N> constexpr auto std::to_array(T (&a)[N]);   // C++20
Name Description
T Element type.
N Number of elements, a constant expression. N == 0 is allowed.
i Zero-based index.
value Value assigned to every element by fill.

Returns: access functions return references; size() returns N; data() returns a pointer to the first element.

Errors / undefined behavior: at(i) throws std::out_of_range when i >= N. operator[] with an invalid index, and front()/back() on an array with N == 0, are undefined. Storing an array<T, N> with a very large N on the stack can overflow it.

Usage notes

  • Size cannot change. There is no push_back; use std::vector for a runtime size.
  • Initialization: std::array<int, 3> a; leaves the elements uninitialized, while std::array<int, 3> a{}; zeroes them. Fewer initializers than N value-initialize the rest.
  • Nested braces: for an array of arrays or structs you may need double braces ({{...}}) in older compilers; the single brace form works with brace elision in modern ones.
  • Zero cost: a[i] compiles to the same code as a raw array; at() adds a check. Under optimization, std::array and C arrays perform identically.
  • No decay: passing a std::array by value copies all of it; pass by reference (const std::array<T, N>&) or take a std::span<const T> to accept any size.
  • Tuple-like: it supports std::get<I>, std::tuple_size and structured bindings.
  • Move cost is O(N) for non-trivially-movable elements: unlike vector, moving an array moves every element.
  • Comparison is element-wise lexicographic; <=> is available since C++20.
  • Iterators are plain pointers in most implementations, and are never invalidated by operations except swap (they then refer to the other array's elements).