std::array in C++
std::array<T, N> from <array> is a fixed-size array with a civilized interface: same zero-overhead contiguous storage as int[N], plus size(), bounds-checked at(), iterators, and no decay when passed around. It is the default choice whenever the size is known at compile time.
Unlike raw arrays it copies and compares with plain = and ==, fills with fill(), and plays with range-based for and STL algorithms directly. If the size isn't compile-time-known, that's vector's job — array and vector split the fixed/dynamic world cleanly between them.
Fixed size in the type
std::array<int, 5> bakes 5 into the type itself — mismatched sizes are distinct, incompatible types.
at() checks bounds
a.at(9) throws std::out_of_range instead of silently corrupting memory like a[9] would.
No decay, real copying
Passing by value copies the whole array safely, and a = b assigns element-wise — raw arrays can do neither.
STL citizen
begin()/end(), size(), empty(), and full algorithm support come free — sort(a.begin(), a.end()) just works.
#include <iostream>#include <array> int main() { std::array<int, 4> a{5, 1, 4, 2}; a[0] = 0; std::cout << a.size() << ", " << a.at(1) << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.