Core · Module 7 · Lesson 1 of 11
vector: The Default Container and How It Grows
Store a growing sequence in contiguous memory, understand doubling growth and amortized cost, and never invalidate an iterator by accident again.
By the end you can
- Explain why vector storage is contiguous and what that buys
- Predict when push_back reallocates and what happens to iterators
- Use reserve, size, capacity and shrink_to_fit correctly
Builds on Virtual Functions and Dynamic Dispatch.
01 · A question first
You push a thousand books into a vector and it never asks for a size up front. Some pushes are instant, one mysteriously copies everything. Where does the extra room come from, what does each push really cost, and why do your iterators sometimes die?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Start with the problem
- How the idea works
- Why it is correct
- What it costs
- Pitfalls and variations
- How reserve keeps pointers steady
- Original cast examples
- Where it is used
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem