std::vector is a dynamic array—the default sequential container in modern C++. It manages memory, grows as needed, and works with algorithms and range-for.
Common operations
std::vector<int> v = {1, 2, 3};
v.push_back(4);
v.size();
v[0]; // unchecked
v.at(0); // bounds-checked
Important interview questions and answers
- Q: vector reallocation?
A:push_backmay reallocate and invalidate iterators—use indices or refresh iterators after growth. - Q: vector vs C array?
A: Vector knows its size, manages lifetime, and integrates with STL algorithms safely.
Self-check
- How do you append an element?
- What happens to iterators on reallocation?
Tip: Call reserve(n) when you know final size—avoids repeated reallocations during push_back.
Interview prep
- vector reallocation?
push_backmay reallocate and invalidate iterators—refresh iterators after growth.- vector vs C array?
Vector tracks size, manages memory, and integrates with STL algorithms.