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.
Syntax
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
Reference
std::array vs C Array vs std::vector
The three are easy to mix up. Here is a quick comparison:
Feature
int a[5]
std::array<int, 5>
std::vector<int>
Size
Fixed
Fixed
Can grow and shrink
Knows its size
No (needs sizeof tricks)
Yes, size()
Yes, size()
Passed to a function
Becomes a pointer
Keeps its type and size
Keeps its type
Copy with =
Not allowed
Yes
Yes
Checked access
None
at()
at()
Memory
Stack
Stack
Heap
Code
C++ Examples
Three short programs that show how std::array works.
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.