0Pricing
C++ Academy · Lesson

Fixed-Size std::array

A safe C-array replacement.

A Safe C-Array

std::array<T, N> is a fixed-size array that knows its own size and behaves like a proper container. Include <array>.

#include <iostream>
#include <array>
int main() {
    std::array<int, 3> a = {1, 2, 3};
    std::cout << a[0] << " " << a.size() << "\n";
}

Knows Its Size

Unlike a raw array that decays to a pointer, std::array always reports its length via size().

#include <iostream>
#include <array>
int main() {
    std::array<double, 4> a{};
    std::cout << a.size() << "\n"; // 4
}

All lessons in this course

  1. Fixed-Size std::array
  2. std::span Basics
  3. Passing Spans to Functions
  4. Bounds and Subspans
← Back to C++ Academy