C++ std::array

Beginner
7 min read
Updated: Nov 2026
3 examples
std::array

Definition and Usage

In C++, std::array<T, N> is a container that holds exactly N elements of type T. The size is fixed: you choose it when you write the code, and it never changes. It lives on the stack like a normal variable, so there is no new and no delete.

You can think of it as a safer C array. It stores elements in one block, just like int arr[5], but it knows its own size, it can be copied and compared, and it comes with helpful member functions such as size(), at(), and fill(). Include <array> to use it.

This page teaches what a std::array is. The Array Functions index lists every function guide in this series.

Fixed size means fixed.

You cannot add or remove elements. If the number of items changes while the program runs, use std::vector instead.

C++ Syntax

Common ways to create and use a std::array:

C++
#include <array>
#include <iostream>

std::array<int, 5> a = {1, 2, 3, 4, 5};  // five ints
std::array<int, 5> zeros{};              // five zeros
std::size_t n = a.size();                // 5
int x = a[0];                            // no bounds check
int y = a.at(0);                         // checked
a.fill(7);                               // all elements become 7

std::array vs C Array vs std::vector

The three are easy to mix up. Here is a quick comparison:

Featureint a[5]std::array<int, 5>std::vector<int>
SizeFixedFixedCan grow and shrink
Knows its sizeNo (needs sizeof tricks)Yes, size()Yes, size()
Passed to a functionBecomes a pointerKeeps its type and sizeKeeps its type
Copy with =Not allowedYesYes
Checked accessNoneat()at()
MemoryStackStackHeap

C++ Examples

Three short programs that show how std::array works.

Example 1 — Create, size(), at(), and fill()

A first program that uses three member functions.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 4> nums = {10, 20, 30, 40};

    std::cout << "size: " << nums.size() << '\n';
    std::cout << "nums.at(2): " << nums.at(2) << '\n';

    nums.fill(0);
    std::cout << "after fill: " << nums.at(2) << '\n';
    return 0;
}

How It Works

  1. std::array<int, 4> makes an array of exactly four ints, and size() reports 4.
  2. at(2) reads the third element, 30, and checks the index at the same time.
  3. fill(0) sets all four elements to 0, so at(2) now gives 0.

Example 2 — Pass an Array to a Function

A std::array is passed with its size, so the function can use size().

C++
#include <array>
#include <iostream>

void printAll(const std::array<int, 3>& values) {
    for (std::size_t i = 0; i < values.size(); ++i) {
        std::cout << values[i] << ' ';
    }
    std::cout << '\n';
}

int main() {
    std::array<int, 3> nums = {7, 8, 9};
    printAll(nums);
    return 0;
}

How It Works

  1. The parameter type std::array<int, 3> includes the size, so nothing is lost when you pass it.
  2. const and & mean the array is not copied and the function cannot change it.
  3. A plain C array would turn into a pointer here, and size() would not be available.

Example 3 — Copy and Compare Arrays

Unlike C arrays, std::array supports = and == directly.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> a = {1, 2, 3};
    std::array<int, 3> b = a;

    b[0] = 99;

    std::cout << std::boolalpha;
    std::cout << "a[0] = " << a[0] << ", b[0] = " << b[0] << '\n';
    std::cout << "a == b: " << (a == b) << '\n';
    return 0;
}

How It Works

  1. b = a copies all three elements, so b is a separate array.
  2. Changing b[0] does not touch a, which still holds 1.
  3. a == b compares element by element, and the first elements now differ, so it is false.

Next: C++ std::array Functions Index

After you know what a std::array is, open the function index for a full tutorial on each member function.

Start withWhy
size()How many elements the array has
at()Read an element with a bounds check
fill()Set every element to one value
begin()Loop with iterators

Full list: C++ std::array Functions index (/cpp/array/function).

Frequently Asked Questions

std::array<T, N> is a container from <array> that holds exactly N elements of type T. The size is fixed at compile time, the elements are stored in one block (usually on the stack), and the object knows its own size.
A C array turns into a pointer when you pass it to a function and does not know its size. A std::array keeps its size, can be copied with =, compared with ==, and has member functions such as size(), at(), and fill().
std::array has a fixed size set at compile time. std::vector can grow and shrink while the program runs and keeps its data on the heap. Use std::array when the number of items never changes.
Only at() checks. nums.at(10) throws std::out_of_range for a bad index, while nums[10] does no check and is undefined behavior.
Open the C++ std::array Functions index. It lists at, front, back, data, empty, size, max_size, the iterator functions, fill, and swap, each with its own tutorial.

Browse every array function

Open the functions index for categorized guides with examples and FAQs.

Array Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful