Time complexity
Time complexity is how the number of steps in an algorithm grows as the input size n grows. We write it in Big-O: std::find on a vector is O(n), std::sort is O(n log n), map lookup is O(log n).
Detailed Explanation
You have a vector of a million users. A linear scan that compares every name will do about a million comparisons. A hash lookup will do a handful. Time complexity is the language we use to say that before we run the program. It ignores constant factors (2n and n are both O(n)) and lower-order terms, and it describes the rate of growth.
Big-O is an upper bound on the worst case, unless we say otherwise. Average case and best case exist (std::sort is O(n log n) average and worst on introsort implementations; a naive quicksort is O(n²) worst). Amortised cost spreads an occasional expensive operation over many cheap ones: vector::push_back is amortised O(1) because reallocations are rare.
Common classes you will see on C++ types:
| Cost | Meaning | Examples |
|---|---|---|
| O(1) | Independent of n | vector[], unordered_map average find |
| O(log n) | Halves the search space | map::find, lower_bound on a sorted vector |
| O(n) | Proportional to n | find, walking a list |
| O(n log n) | Sort-shaped | std::sort, stable_sort |
| O(n²) | Nested loops over n | naive pairwise compare |
| O(2^n) | Exponential | unmemoised subset enumeration |
C++ makes the constant factors visible. An O(n) walk of a vector and an O(n) walk of a list have the same Big-O and wildly different wall times, because the vector is contiguous. Big-O does not replace measurement. It tells you which algorithm family you are in, and whether a 1000× larger input will finish.
When you write an API, document the complexity if it is not obvious. The standard already does: std::list::size is O(1) since C++11; std::forward_list has no size. std::distance is O(1) for random-access iterators and O(n) otherwise.
Nested loops multiply. A loop over n that calls an O(n) function is O(n²). A loop over n that does a map lookup is O(n log n). Recursion with two subcalls of size n−1 is O(2^n) unless you share work.
Do not micro-optimise an O(n²) algorithm into a faster O(n²) algorithm when an O(n log n) one exists. Change the class first, then the constants.
Code Examples
#include <cstddef>
#include <iostream>
#include <vector>
int indexOf(const std::vector<int>& v, int target) { // O(n)
for (int i = 0; i < static_cast<int>(v.size()); ++i) {
if (v[static_cast<std::size_t>(i)] == target) return i;
}
return -1;
}
int main() {
std::vector<int> v{4, 8, 15, 16};
std::cout << indexOf(v, 15) << '\n'; // prints: 2
}#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{4, 8, 15, 16};
std::sort(v.begin(), v.end()); // O(n log n)
std::cout << std::binary_search(v.begin(), v.end(), 15) << '\n'; // O(log n)
}Common Gotchas
Average is not worst
`unordered_map::find` is O(1) average and O(n) if every key landed in one bucket. Do not put untrusted keys in an unsalted table and call it O(1).
Big-O hides constants
`std::sort` on 20 ints can lose to an insertion sort. Complexity is a large-n statement.
Iterator category changes the cost
`std::distance(list.begin(), list.end())` walks the list. Do not wrap it in a condition you think is cheap.