Skip to content

std::array (C++11) ​

In a Nutshell ​

A fixed-size array that never decays into a pointer: it delivers the performance of a C-style array while supporting standard container interfaces such as size(), iterators, and assignment.

Header File ​

#include <array>

Core API Cheat Sheet ​

OperationSignatureDescription
Element accessreference at(size_type pos)Bounds-checked element access
Element accessreference operator[](size_type pos)Unchecked element access
First elementreference front()Accesses the first element
Last elementreference back()Accesses the last element
Underlying pointerT* data() noexceptDirect access to the underlying array pointer
Fillvoid fill(const T& value)Fills every element with the given value
Sizeconstexpr size_type size() noexceptReturns the number of elements (a compile-time constant)
Empty checkconstexpr bool empty() noexceptChecks whether the array is empty (true when N == 0)
Swapvoid swap(array& other)Swaps the contents of two arrays
Begin iteratoriterator begin() noexceptReturns an iterator pointing to the beginning

Minimal Example ​

C++
#include <array>
#include <iostream>
// Standard: C++11
int main() {
    std::array<int, 3> arr = {1, 2, 3};
    arr.fill(0);
    arr[0] = 42;
    for (const auto& v : arr)
        std::cout << v << ' '; // outputs: 42 0 0
    std::cout << "\nsize: " << arr.size(); // outputs: size: 3
}

Embedded Suitability: High ​

  • A zero-overhead abstraction: after compilation it is identical to a C-style array and introduces no heap allocation
  • size() is a compile-time constant, usable in template metaprogramming and static assertions
  • Supports constexpr, making it a good fit for building lookup tables at compile time
  • The built-in bounds check in at() is convenient for debugging and can be removed in Release builds

Compiler Support ​

GCCClangMSVC
4.43.119.0

See Also ​


Part of the content is referenced from cppreference.com and is licensed under CC-BY-SA 4.0

pdf-latest-4-g85128cc · 85128cc · 2026-10-05