01 / 21
\( O(n) \)
<vector>

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.

C++

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.

\( O(1) \)
Constant: time never changes regardless of \( n \).
\( O(n) \)
Linear: time grows directly with \( n \).
\( O(n^2) \)
Quadratic: time grows with the square of \( n \).

Visualizing Growth Rates

Input size n Time 20 40 60 80
\( O(1) \)
\( O(\log n) \)
\( O(n) \)
\( O(n \log n) \)
\( O(n^2) \)

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.

1
Scalability: Code that works for 100 items may freeze at 1,000,000 items if the algorithm is \( O(n^2) \).
2
Container choice: std::vector, std::map, and std::unordered_map all have different Big-O for the same operations.
3
Interview & competition standard: Knowing Big-O is expected in technical interviews and competitive programming.

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 \).

No loop
Runs once: \( O(1) \)
One loop 0..n
Runs \( n \) times: \( O(n) \)
Nested loops
Outer \( \times \) inner: \( O(n^2) \)

Halving Loops give \( O(\log n) \)

int i = n; while (i > 0) { // do work i /= 2; }

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

std::vector Access by index
\( O(1) \)
push_back
\( O(1) \)*
Insert middle
\( O(n) \)
Find (unsorted)
\( O(n) \)
std::map Insert
\( O(\log n) \)
Find
\( O(\log n) \)
Erase
\( O(\log n) \)
Iterate all
\( O(n) \)
unordered_map Insert
\( O(1) \)*
Find
\( O(1) \)*
Erase
\( O(1) \)*
Iterate all
\( O(n) \)
std::set Insert
\( O(\log n) \)
Find
\( O(\log n) \)
Erase
\( O(\log n) \)
Access by index
N/A
std::list push_front/back
\( O(1) \)
Insert at iterator
\( O(1) \)
Find
\( O(n) \)
Access by index
\( O(n) \)
* = amortized

How to Analyze Your Code

1
Identify the loops. Count every for, while, and recursive call.
2
Check nesting. Nested loops multiply: a loop inside a loop over \( n \) is \( O(n^2) \).
3
Check each container call. Replace .find(), .insert(), etc. with their Big-O from the table.
4
Multiply loop cost by body cost. A loop of \( n \) that calls map::find each iteration is \( O(n \log n) \).
5
Add sequential blocks, keep the largest. \( O(n) + O(n^2) = O(n^2) \).

Example 1: Single Loop

int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; // O(1) per iteration }
n iterations
\( O(1) \)
\( O(1) \)
...
\( O(1) \)
Total: \( O(n) \)

Example 2: Nested Loops

for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << i * j; // O(1) } }
Outer: n times Inner: n times
\( O(1) \)
=
\( n \times n \times 1 \)
\( O(n^2) \)

Example 3: Loop + Container Call

std::map<int, int> m; for (int i = 0; i < n; i++) { m[arr[i]]++; // map::operator[] is O(log n) }

Sequential vs Nested

// Block A: O(n) for (int i = 0; i < n; i++) { ... } // Block B: O(n^2) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { ... }
Sequential (A then B)
\( O(n) + O(n^2) = O(n^2) \). The larger term dominates.
Nested (A inside B)
\( O(n) \times O(n^2) = O(n^3) \). This would be much worse.

Choosing the Right Container

Need fast lookup? Yes No Need sorted order? Yes No std::map
\( O(\log n) \) lookup
unordered_map
\( O(1) \) avg lookup
Index access? Yes No std::vector
\( O(1) \) index
std::list
\( O(1) \) insert
Default choice: std::vector Cache-friendly, fast for most use cases

Common Mistakes

✕ Ignoring container cost
Calling .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) \).
✕ Confusing add vs multiply
Two sequential loops are \( O(n) + O(n) = O(n) \), not \( O(n^2) \). Only nested loops multiply.
✕ Forgetting amortized cost
Saying 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?

Question 2 of 5

What is the total time complexity of this code?

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?

Question 5 of 5

What is the time complexity of this loop?