Language collection paths · 6 steps
C++ collections
The five core C++ collections in the order you need them, each paired with the structure underneath, a visualizer and a problem that uses it.
- Who it is for
- C++ developers who want to know what the standard collections cost and which one to reach for.
- By the end
- You can choose between the core C++ collections by operation cost and avoid the usual misuse of each.
Step 1: How to choose a collection
Big O and the built-in collections module explain the operation costs that every step below relies on.
- ModuleM03 Big O· beginner
- ModuleM04 Built-in Collections· beginner
Step 2: Dynamic array
The default sequence. push_back is amortized O(1); a reallocation invalidates every iterator, pointer and reference into the vector.
- Collectionstd::vector<T>· C++
- VisualizerDynamic array· append amortized O(1); insert/remove O(n)
- LessonDynamic arrays: capacity, resizing and amortized append· M05 Arrays
- Complexity exerciseInserting at the front of an array list, n times· Θ(n²)
Step 3: Hash map
Lookup by key in expected O(1). operator[] inserts a default value when the key is missing; use find or contains (C++20) to only look.
- Collectionstd::unordered_map<Key, T>· C++
- VisualizerHash table· put/get/remove expected O(1)
- PatternHashing for constant-time lookup· O(n) expected
- ProblemTwo readings that add up to a target· beginner · O(n) expected
Step 4: Hash set
Membership without values. insert returns a pair whose bool says whether the value was new.
- Collectionstd::unordered_set<Key>· C++
- ProblemHas any badge been scanned twice?· beginner · O(n) expected
Step 5: Queue and deque
First in, first out, the order BFS needs. std::deque is O(1) at both ends but not contiguous; std::queue adapts it by default.
- Collectionstd::deque<T>· C++
- VisualizerQueue (ring buffer)· enqueue/dequeue O(1)
- PatternBreadth-first search and level order· O(V + E)
Step 6: Priority queue
Repeatedly take the smallest (or largest) item. priority_queue is a max-heap by default; pass std::greater<> for a min-heap.
- Collectionstd::priority_queue<T>· C++
- PatternTop K with a heap· O(n log k)
- ModuleM21 Heaps and Priority Queues· intermediate