Big-O for
C++ Containers
Learn how to analyze the time complexity of loops and common container operations: why some code flies and other code crawls.
What is Big-O Notation?
Big-O notation describes how the runtime of an algorithm grows as the input size \( n \) increases. It focuses on the worst-case upper bound and ignores constant factors.
Visualizing Growth Rates
Why Big-O Matters in C++
The C++ Standard Library gives you many containers, each with different performance guarantees. Picking the wrong one can turn a millisecond operation into a minute-long wait.
std::vector, std::map, and std::unordered_map all have different Big-O for the same operations.The Core Rule: Count the Loops
The simplest way to determine Big-O is to count how many times the innermost statement runs relative to \( n \).
Halving Loops give \( O(\log n) \)
Each iteration halves the remaining work. Starting from \( n \), it takes \( \log_2 n \) halvings to reach 0. This is the pattern behind binary search.
Amortized vs Worst-Case
Some operations, like push_back on a std::vector, are usually \( O(1) \) but occasionally \( O(n) \) when the vector resizes. Averaged over many calls, the cost per call is still \( O(1) \): this is amortized analysis.
| Amortized | Worst-Case | |
|---|---|---|
| vector::push_back | \( O(1) \) | \( O(n) \) |
| unordered_map::insert | \( O(1) \) | \( O(n) \) |
| map::insert | \( O(\log n) \) | \( O(\log n) \) |
C++ Container Operation Costs
How to Analyze Your Code
for, while, and recursive call..find(), .insert(), etc. with their Big-O from the table.map::find each iteration is \( O(n \log n) \).Example 1: Single Loop
Example 2: Nested Loops
Example 3: Loop + Container Call
Sequential vs Nested
Choosing the Right Container
Common Mistakes
.find() inside a loop without checking its Big-O. A map::find in a loop of \( n \) makes it \( O(n \log n) \), not \( O(n) \).vector::push_back is \( O(n) \) because resizing can happen. It is \( O(1) \) amortized, which is what matters for total cost.Summary Table
| Container | Access | Insert | Find | Erase |
|---|---|---|---|---|
| vector | \( O(1) \) | \( O(n) \)* | \( O(n) \) | \( O(n) \) |
| list | \( O(n) \) | \( O(1) \) | \( O(n) \) | \( O(1) \)* |
| map / set | N/A | \( O(\log n) \) | \( O(\log n) \) | \( O(\log n) \) |
| unordered_map / _set | N/A | \( O(1) \)* | \( O(1) \)* | \( O(1) \)* |
Question 1 of 5
What is the time complexity of accessing an element by index in a std::vector?
std::vector stores elements in contiguous memory, so jumping to any index is a single pointer arithmetic operation: \( O(1) \).Question 2 of 5
Code snippet:
for (int i = 0; i < n; i++)
myMap.insert({arr[i], i});
where myMap is a std::map<int, int>.
What is the total time complexity of this code?
map::insert is \( O(\log n) \). Multiply: \( n \times O(\log n) = O(n \log n) \).Question 3 of 5
Two sequential (non-nested) loops each run \( n \) times. What is the combined complexity?
Question 4 of 5
Which container gives the fastest average-case lookup by key?
std::unordered_map uses a hash table, giving \( O(1) \) average-case lookup. std::map uses a balanced BST at \( O(\log n) \), and both vector and list require \( O(n) \) linear search.Question 5 of 5
Code snippet:
int i = n;
while (i > 0) {
cout << i;
i /= 2;
}
What is the time complexity of this loop?