1/20
Memorizing data structures, remembering the time complexities, ups and downsides, and when to use one over the other
std::vector
Dynamic array; O(1) amortized push_back and O(1) random access by index, O(n) insert or erase in the middle.
std::list
Doubly linked list; O(1) insert or erase at both ends and O(1) insert or erase at a known iterator, but O(n) random access.
std::deque
Double-ended queue; O(1) push or pop at both front and back, O(1) random access, O(n) insert in the middle.
std::set
Ordered set implemented as a red-black tree; O(log n) insert, erase, and find, with elements kept in sorted order.
std::unordered_set
Hash-based set with unique keys; O(1) average insert, erase, and find, but O(n) worst case and no sorted order.
std::map
Ordered key-value map implemented as a red-black tree; O(log n) insert, erase, and lookup by key.
std::unordered_map
Hash-based key-value map; O(1) average insert, erase, and lookup by key, but O(n) worst case and no sorted order.
std::stack
Container adapter with LIFO access; O(1) push, pop, and top.
std::queue
Container adapter with FIFO access; O(1) push, pop, front, and back.
std::priority_queue
Max-heap priority queue; O(log n) push and pop, O(1) top. Use when you repeatedly need the largest element.
std::array
Fixed-size array; O(1) random access and O(1) size, with no resizing. Use when the size is known at compile time.
Disjoint Set Union (Union-Find)
Disjoint set union (DSU); near O(α(n)) per union and find operation with path compression and union by size. Use for dynamic connectivity and grouping problems.
Binary search on a sorted vector
Sorted array searched by halving the range; O(log n) lookup and O(n) insertion. Use when the data is static and sorted.
Trie
Prefix tree for strings; O(L) insert and search, where L is the string length. Use for prefix queries and autocomplete.
Segment tree
Tree of range aggregates; O(log n) point update and range query, and O(n) build. Use for range sum or min queries with updates.
Prefix sum array
Array of prefix sums; O(1) range sum query after O(n) build, but no efficient updates. Use for static range sum queries.
std::multiset
Ordered multiset implemented as a red-black tree; allows duplicate keys, stores equal keys together in sorted order, O(log n) insert and find, and erase by key removes all copies.
std::unordered_multiset
Hash-based multiset that allows duplicate keys; O(1) average insert, erase, and find, but O(n) worst case and no sorted order.
Key difference between std::set and std::multiset
std::set stores each key at most once, while std::multiset allows duplicate keys; both keep elements sorted with O(log n) insert, erase, and find.
Situation that calls for std::multiset instead of std::set
A sorted collection that must keep repeated values, such as tracking every occurrence of a value while still answering smallest or largest queries efficiently.
Removing only one copy of a key from std::multiset
Call find(x) to get an iterator to one copy of x, then call erase on that iterator; O(log n) total. Calling erase(x) directly removes every copy of x.