C++ Containers Index

1 minute read

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.

Core Sequence Containers

  1. Vector and String
  2. Deque and Sliding Windows
  3. List and LRU Patterns

Associative Containers

  1. Maps and Hashing
  2. Sets and Multisets

Container Adapters

  1. Queue, Stack, and Priority Queue

Iterator Rules

  1. Iterators and Invalidation

Container Selection

NeedContainer
Dynamic array, index accessvector
Text manipulationstring
Key-value lookup, sorted keysmap
Key-value lookup, average O(1)unordered_map
Unique values, sorted orderset
Sorted values with duplicatesmultiset
Unique values, average O(1)unordered_set
Stable iterators, node movementlist
FIFO processingqueue
LIFO processingstack
Max/min element accesspriority_queue
Push/pop from both endsdeque

Complexity Summary

ContainerAccessInsertDeleteLookup
vectorO(1) by indexO(1) end, O(n) middleO(1) end, O(n) middleO(n)
stringO(1) by indexO(1) end, O(n) middleO(1) end, O(n) middleO(n)
map-O(log n)O(log n)O(log n)
unordered_map-O(1) averageO(1) averageO(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) averageO(1) averageO(1) average
listO(n) by traversalO(1) with iteratorO(1) with iteratorO(n)
queuefront/back onlyO(1)O(1)-
stacktop onlyO(1)O(1)-
priority_queuetop onlyO(log n)O(log n)-
dequeO(1) by indexO(1) endsO(1) endsO(n)

Interview Checklist

  • Use vector when index access and traversal are central.
  • Use unordered_map or unordered_set when fast lookup is central and order does not matter.
  • Use map, set, or multiset when sorted order or range queries matter.
  • Use queue for BFS and stack for iterative DFS or nested parsing.
  • Use priority_queue for repeated best/min/max extraction.
  • Use deque for sliding-window and monotonic-queue patterns.
  • Use list only when stable iterators or node movement matter.
  • Be careful: map[key] and unordered_map[key] insert default values when the key is missing.
  • Be careful: vector reallocation invalidates existing pointers, references, and iterators.

Leave a Comment