C++ Containers Index
Published:
This is the central index for the C++ containers series. The goal is to keep the overview short and link to focused notes for each container family.
The series uses C++20 as its default teaching baseline. Operations introduced in C++20 or later are labelled where they appear; the core containers and most of their interfaces are available in earlier standards.
Core Sequence Containers
Associative Containers
Container Adapters
Iterator Rules
Container Selection
| Need | Container |
|---|---|
| Dynamic array, index access | vector |
| Text manipulation | string |
| Key-value lookup, sorted keys | map |
Key-value lookup, average O(1) | unordered_map |
| Unique values, sorted order | set |
| Sorted values with duplicates | multiset |
Unique values, average O(1) | unordered_set |
| Stable iterators, node movement | list |
| FIFO processing | queue |
| LIFO processing | stack |
| Max/min element access | priority_queue |
| Push/pop from both ends | deque |
Complexity is only one selection criterion. Also consider:
- Access pattern: indexed access, sequential traversal, ordered lookup, or membership only.
- Storage: contiguous elements improve locality and interoperability with pointer-based APIs; node-based containers provide stable element addresses at an allocation cost.
- Ordering: ordered associative containers support bounds and range traversal; unordered containers do not.
- Mutation: insertion and erasure may invalidate iterators, references, and pointers differently.
- Cardinality: choose a unique-key or duplicate-key container deliberately.
- Workload evidence: constant factors, allocation volume, key distribution, and cache behavior can outweigh asymptotic notation.
Complexity Summary
| Container | Access | Insert | Delete | Lookup |
|---|---|---|---|---|
vector | O(1) by index | O(1) end, O(n) middle | O(1) end, O(n) middle | O(n) |
string | O(1) by index | O(1) end, O(n) middle | O(1) end, O(n) middle | O(n) |
map | - | O(log n) | O(log n) | O(log n) |
unordered_map | - | O(1) average | O(1) average | O(1) average |
set | - | O(log n) | O(log n) | O(log n) |
multiset | - | O(log n) | O(log n) | O(log n) |
unordered_set | - | O(1) average | O(1) average | O(1) average |
list | O(n) by traversal | O(1) with iterator | O(1) with iterator | O(n) |
queue | front/back only | O(1) | O(1) | - |
stack | top only | O(1) | O(1) | - |
priority_queue | O(1) top | O(log n) | O(log n) | - |
deque | O(1) by index | O(1) ends | O(1) ends | O(n) |
Engineering Checklist
- Use
vectorwhen index access and traversal are central. - Use
unordered_maporunordered_setwhen fast lookup is central and order does not matter. - Use
map,set, ormultisetwhen sorted order or range queries matter. - Use
queuefor FIFO work andstackfor LIFO work when their restricted interfaces express the intended invariant. - Use
priority_queuefor repeated best/min/max extraction. - Use
dequewhen efficient mutation at both ends is required. - Use
listonly when stable iterators or node movement matter. - Be careful:
map[key]andunordered_map[key]insert default values when the key is missing. - Be careful: vector reallocation invalidates existing pointers, references, and iterators.
- Check empty-container preconditions before
front,back,top, orpopwhen emptiness is possible. - Treat complexity tables as a starting point, then measure representative data and operations.
Leave a Comment