Skip to content

C++ vector vs array: which one should you use?

When to use std::vector and when to use std::array in C++, with simple examples and a quick rule of thumb.

Published 2026-11-28

std::array has a fixed size known when you write the code. std::vector can grow and shrink while the program runs.

Rule of thumb: use std::vector by default. Use std::array when the size never changes, like the 7 days of a week.

Both know their own size(), work with range-based for loops and can be passed to STL algorithms — much safer than old C-style arrays.

#include <array>
#include <vector>
std::array<int, 3> fixed{1, 2, 3};
std::vector<int> grow{1, 2};
grow.push_back(3);

Learn it properly — free lessons

← All posts