Skip to main content
๐Ÿ“œ 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.

  1. How do you access it? By position โ†’ vector (or array if fixed size). By key โ†’ map/unordered_map. By discipline only โ†’ adapter.
  2. Which ends? Both ends O(1) โ†’ deque. Middle splicing with stable iterators โ†’ list. Everything else โ†’ vector.
  3. Is sorted order part of the contract? Yes โ†’ ordered family. No โ†’ unordered_map for 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.