Choosing the Right Container
Decision guide
- Fixed size known at compile time? →
std::array - A sequence where order = insertion/position?
- Mostly add/remove at the end, index access →
std::vector✅ (default) - Add/remove at both ends →
std::deque - Lots of insert/erase in the middle with stable iterators, splicing →
std::list
- Mostly add/remove at the end, index access →
- Look up by key?
- Need sorted order, ranges, min/max key →
std::map/std::set - Just fast lookup →
std::unordered_map/std::unordered_set - Duplicate keys →
multimap/unordered_multimap
- Need sorted order, ranges, min/max key →
- Restricted access pattern? LIFO →
stack; FIFO →queue; highest first →priority_queue - Just viewing someone else's data? →
std::span,std::string_view
Complexity summary
| Container | Access | Search | Insert/erase | Notes |
|---|---|---|---|---|
vector | O(1) | O(n) | end O(1)*, middle O(n) | best cache use |
deque | O(1) | O(n) | ends O(1) | |
list | O(n) | O(n) | O(1) at iterator | stable iterators |
set/map | — | O(log n) | O(log n) | sorted |
unordered_* | — | O(1) avg | O(1) avg | unordered |
priority_queue | top O(1) | — | O(log n) |
Practical tips ✅
- When unsure, start with
vector; measure before switching - A sorted
vector+std::lower_boundbeatsstd::setfor data that is built once and searched often - C++23
std::flat_map/std::flat_setpackage that idea (sorted vectors with a map interface) reservevectors and unordered containers when you know sizes- Prefer values in containers; use
unique_ptronly for polymorphic objects
Big-O isn't everything
Constant factors and memory layout matter: vector's contiguous memory makes it faster than list for many operations that are "O(n) vs O(1)" on paper, at typical sizes.
Example
#include <algorithm>
#include <chrono>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
int main() {
// Same task, three containers: "is this transaction id known?" for 100,000 ids
const int N = 100'000;
std::vector<int> ids(N);
for (int i = 0; i < N; ++i) ids[i] = i * 7;
std::vector<int> sorted_vec = ids; // sorted already
std::set<int> tree(ids.begin(), ids.end());
std::unordered_map<int, bool> hash;
hash.reserve(N);
for (int id : ids) hash[id] = true;
int queries[] = { 0, 7 * 54321, 7 * 99999, 5, 123456 };
std::cout << "lookup results (sorted vector / set / unordered_map):\n";
for (int q : queries) {
bool a = std::binary_search(sorted_vec.begin(), sorted_vec.end(), q);
bool b = tree.contains(q);
bool c = hash.contains(q);
std::cout << " " << q << ": " << a << ' ' << b << ' ' << c << '\n';
}
// Ordered output: only the tree keeps keys sorted for free
std::map<std::string, int> sorted_spend = { { "Travel", 800 }, { "Food", 1650 }, { "Bills", 2300 } };
std::cout << "map iterates sorted:";
for (const auto& [k, v] : sorted_spend) std::cout << ' ' << k;
std::cout << '\n';
// Middle insertion: list keeps iterators, vector shifts elements
std::vector<int> vec = { 1, 2, 4, 5 };
vec.insert(std::lower_bound(vec.begin(), vec.end(), 3), 3); // keep the vector sorted
std::list<int> lst = { 1, 2, 4, 5 };
lst.insert(std::next(lst.begin(), 2), 3);
std::cout << "vector:";
for (int x : vec) std::cout << ' ' << x;
std::cout << " | list:";
for (int x : lst) std::cout << ' ' << x;
std::cout << '\n';
std::cout << "memory per element (approx): vector " << sizeof(int)
<< " B, list " << sizeof(int) + 2 * sizeof(void*) << "+ B, set " << sizeof(int) + 3 * sizeof(void*) << "+ B\n";
return 0;
}
/* Output:
lookup results (sorted vector / set / unordered_map):
0: 1 1 1
380247: 1 1 1
699993: 1 1 1
5: 0 0 0
123456: 0 0 0
map iterates sorted: Bills Food Travel
vector: 1 2 3 4 5 | list: 1 2 3 4 5
memory per element (approx): vector 4 B, list 20+ B, set 28+ B
*/