Caffeinated Coder
Book an appointment
Lesson 19 · Modern C++

STL containers: how std::vector grows

The standard library's containers are objects that own a group of elements and manage their memory for you. Follow the one you will use most, std::vector, through an eleven-line program: a small object on the stack, its elements side by side on the heap, push_back filling spare capacity, and the moment the block is full and the vector moves everything to a bigger one. See why growing invalidates old pointers, why push_back is still fast on average, and how the destructor frees it all.

12 min10 steps5 questionsAdvanced
Fig 19 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.