Choosing
Container Types
Learn how to pick the right C++ container for the job by understanding access patterns, performance trade-offs, and real-world scenarios.
What Is a Container?
A container is an object that stores a collection of other objects (its elements). The C++ Standard Library provides many container types, each with different strengths. Choosing the right one directly affects your program's performance and clarity.
The Big Three Categories
Why the Choice Matters
Different containers have vastly different performance for the same operation. Choosing poorly can turn an \( O(1) \) lookup into an \( O(n) \) scan, or make insertions hundreds of times slower.
The Core Decision Rule
Sequence Containers Deep Dive
Associative vs Unordered
| Associative (map/set) | Unordered (unordered_map/set) | |
|---|---|---|
| Structure | Balanced BST (red-black tree) | Hash table |
| Lookup | \( O(\log n) \) | \( O(1) \) average |
| Insert | \( O(\log n) \) | \( O(1) \) average |
| Ordered iteration | Yes (sorted) | No |
| Worst case | \( O(\log n) \) guaranteed | \( O(n) \) on hash collision |
How to Choose: Step by Step
Example 1: Game Leaderboard
Scenario: You are building a game leaderboard. You need to store player names with their scores, display the top 10 at any time, and quickly update a player's score.
Example 2: Spell Checker
Scenario: A spell checker must quickly determine whether a word exists in a 100,000-word dictionary. Order does not matter: you just need "is this word valid?"
Example 3: Undo History
Scenario: A text editor needs an undo feature. The most recent action is undone first (LIFO). You only ever access the top element.
Memory Layout Matters
Big-O at a Glance
| Operation | vector | deque | list | unordered_map |
|---|---|---|---|---|
| Access [i] | \( O(1) \) | \( O(1) \) | \( O(n) \) | N/A |
| Push back | \( O(1)^* \) | \( O(1) \) | \( O(1) \) | N/A |
| Push front | \( O(n) \) | \( O(1) \) | \( O(1) \) | N/A |
| Insert mid | \( O(n) \) | \( O(n) \) | \( O(1) \) | N/A |
| Find by key | \( O(n) \) | \( O(n) \) | \( O(n) \) | \( O(1) \) |
When vector Wins
Even for operations where list has a better theoretical Big-O (like mid-insertion), vector often wins in practice for small to medium sizes due to cache locality and low overhead.
Common Mistakes
Question 1 of 5
You need to store 10,000 student IDs and look each one up by ID number as fast as possible. Order does not matter. Which container is best?
Question 2 of 5
A music playlist needs to display songs in alphabetical order and allow adding new songs efficiently. Which container fits best?
Question 3 of 5
What is the average time complexity of looking up a key in an std::unordered_map?
Question 4 of 5
You are processing a stream of data and need to remove elements from the front and add elements to the back as fast as possible. Which container is ideal?
Question 5 of 5
Why is std::vector often faster than std::list in practice, even for operations where list has a better Big-O?