Module 4 · Pointers, Arrays, Strings and Structures — topic 2 of 4
Arrays, std::array and std::vector
Built-in arrays, the fixed-size std::array and the resizable std::vector: declaration, access, size, passing to functions and choosing the right one.
Learning objectives
- Use built-in arrays, std::array and std::vector
- Explain size(), push_back, at() and bounds checking
- Choose the right container for a task
Definition
A built-in array is a fixed-size contiguous sequence of elements.std::array<T, N>wraps it with a size and value semantics;std::vector<T>is a dynamically resizable contiguous array that manages its own memory.
Introduction
C++ inherits C arrays, with all their problems: they don't know their own size, decay to pointers, and can't be copied or returned. The standard library offers two better options: std::array for fixed sizes and std::vector when the size changes.
Why it matters
std::vector is the default container in C++ and appears in almost every program and project.
Using std::array/std::vector removes the decay and size-tracking bugs of C arrays.
Detailed explanation
int a[5]{}; is a built-in array; std::size(a) (C++17) gives 5.
std::array<int, 5> b{}; has b.size(), can be copied with =, compared with == and returned from functions.
std::vector<int> v; starts empty; push_back appends; size() counts; resize and reserve manage capacity. Memory is freed automatically.
v[i] is unchecked (out of range is undefined behaviour); v.at(i) throws std::out_of_range when i is invalid.
Syntax
- Include
<array>and<vector>. - Pass vectors by (const) reference.
- Prefer range-for or algorithms to index loops.
Code examples
std::vector basics
3 75 95
std::array copies and compares
1 9 0 3
More examples
Bounds checking and capacity
size 4, capacity >= 4: 1 caught out_of_range 5 10 30 40
Step-by-step explanation
reserve(4)allocates room for four elements up front.at(10)is out of range and throws instead of reading random memory.eraseremoves the element at position 1 (20), shifting the rest.insertatbegin()shifts everything right.
Expected output
Running “Bounds checking and capacity” prints exactly:
Practical applications
- Game engines store entities in vectors for cache-friendly iteration.
std::arrayholds fixed-size data such as RGB colours or 3D coordinates.- Competitive programmers use vectors for nearly every array.
Best practices
- Default to
std::vector. - Use
std::arrayfor small, fixed sizes. - Use
at()while debugging or for untrusted indexes.
Common mistakes
Indexing an empty vector
Avoid
Prefer
An empty vector has no element 0; v[0] is undefined behaviour.
Passing a vector by value
Avoid
Prefer
By value copies every element on each call.
Important notes
- push_back may reallocate, invalidating pointers, references and iterators to elements.
std::vector<bool>is a special bit-packed specialisation that does not behave exactly like other vectors.
Key takeaways
- Prefer std::vector / std::array to built-in arrays.
at()is bounds-checked;[]is not.- Pass containers by const reference.
Try it yourself
- 1Read numbers until end of input into a vector and print them in reverse.
- 2Remove all even numbers from a vector using
erasein a loop (orstd::erase_ifin C++20).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat happens with `v.at(10)` on a vector of size 3?
- BeginnerWhich is a key advantage of std::array<int, 5> over int[5]?