๐ WAYPOINT LESSON
Choosing the Right Container
โญโญ intermediateโณ 20 min read๐ Lesson 95 of 204
A three-question checklist plus one complexity table covering every container this course has met.
By now the STL menu is: sequence containers, adapters, and the associative families. Choosing is a three-question checklist.
- How do you access it? By position โ
vector(orarrayif fixed size). By key โmap/unordered_map. By discipline only โ adapter. - Which ends? Both ends O(1) โ
deque. Middle splicing with stable iterators โlist. Everything else โvector. - Is sorted order part of the contract? Yes โ ordered family. No โ
unordered_mapfor lookup speed.
Complexity in one table:
| Operation | vector | deque | list | map | unordered_map | |---|---|---|---|---|---| | index/front/back | O(1) | O(1) | O(n) | O(log n) | O(1) avg | | insert at ends | O(1)* | O(1) | O(1) | O(log n) | O(1) avg | | find by value/key | O(n) | O(n) | O(n) | O(log n) | O(1) avg |
* amortized at the back.
The default plan in real code: vector by default, unordered_map for
lookups, map when output must be sorted, priority_queue for "best first"
processing, deque for sliding windows.