A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Standard Libraries & Collections

STL

Medium

The Standard Template Library is C++'s built-in arsenal: ready-made containers (vector, map, set, stack, queue, priority_queue), iterators that connect them, and algorithms (sort, binary search, accumulate) that work on all of them. Learning the STL is the single highest-leverage step in C++ DSA — most 'hard' implementation work becomes a few library calls, and your effort moves to the actual logic.

The mental model: containers, iterators, algorithms

Containers own the data and each has a personality: vector is a growable array, map is a sorted key-value tree, unordered_map is a hash table, set keeps unique sorted values, priority_queue always exposes the largest. Iterators are generalised pointers that walk a container. Algorithms are free functions — sort(v.begin(), v.end()) — that operate on iterator ranges, so one algorithm serves every container.

Everything in the STL has a known complexity, and choosing a container IS choosing your complexities: the table later in this lesson is the part to internalise.

vector and pair: the daily drivers

vector<T> is your default sequence: push_back appends in amortised O(1), v[i] accesses in O(1), v.size() reports length, and sort + reverse give you the two most used algorithms. pair<T,U> bundles two values (access via .first and .second) and compares lexicographically, which makes vector<pair<int,int>> the standard trick for 'sort by one field, break ties by another'.

#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {5, 2, 9, 2}; sort(v.begin(), v.end()); // 2 2 5 9 v.erase(unique(v.begin(), v.end()), v.end()); // 2 5 9 cout << v.size() << '\n'; // 3 cout << accumulate(v.begin(), v.end(), 0) << '\n'; // 16 return 0; }

map vs unordered_map, set vs unordered_set

map (balanced tree) keeps keys sorted and guarantees O(log n) for insert/find/erase; unordered_map (hash table) averages O(1) but has no order and a bad worst case. Rule of thumb: need sorted order, predecessor/successor, or range queries → map; need pure frequency counting or membership at scale → unordered_map. The same choice exists for set/unordered_set when you store keys without values.

Frequency counting is the most common STL pattern in this whole sheet:

unordered_map<int, int> freq; for (int x : nums) freq[x]++; int best = nums[0]; for (auto &p : freq) if (p.second > freq[best]) best = p.first; cout << best; // highest occurring element

stack, queue, priority_queue

stack<T> gives push/pop/top in O(1) — last in, first out; it powers bracket matching and 'previous greater element' problems. queue<T> is first in, first out for BFS-style level processing. priority_queue<T> is a max-heap: top() is the largest element, push/pop cost O(log n); for a min-heap write priority_queue<int, vector<int>, greater<int>>. 'Top K' problems are almost always a heap of size K — keep the K best, evict the worst.

Algorithms worth memorising

sort(first, last) and sort with a comparator lambda for custom order (e.g. by second element, or descending). binary_search, lower_bound (first element ≥ x) and upper_bound (first > x) on any sorted range, all O(log n). max_element / min_element return iterators — dereference with *. accumulate sums a range (start it with 0LL to avoid int overflow). reverse, count, find and __gcd(a, b) round out the everyday set.

Complexity summary (text table): vector access O(1) | push_back O(1) amortised | insert middle O(n) map / set: O(log n) everything, keys stay sorted unordered_map / unordered_set: average O(1), no order priority_queue: top O(1), push/pop O(log n) sort O(n log n) | binary_search/lower_bound O(log n) on sorted data

Common mistakes

Using map where only counting is needed — you pay log factors for order you never use. Forgetting that unordered_map with adversarial keys can degrade (rare in practice, real in contests). Erasing from a vector inside a loop and invalidating iterators — erase returns the next valid iterator; use it. Sorting a vector of pairs expecting stability that is not guaranteed. And the quiet classic: accumulate with an int initial value overflowing silently on large sums.

Key points

  • STL = containers + iterators + algorithms; picking a container means picking your complexities.
  • vector is the default sequence; pair compares lexicographically, ideal for tie-broken sorting.
  • map/set: sorted, guaranteed O(log n). unordered_map/set: average O(1), no order — use for counting and membership.
  • priority_queue is a max-heap (flip with greater<int>); top-K problems = a heap of size K.
  • Memorise: sort, lower_bound/upper_bound, binary_search, max_element, accumulate (start with 0LL), __gcd.
  • Frequency counting with unordered_map is the most reused pattern in the entire sheet.

Suggested questions

Highest Occurring Element in an Array ↗ The exact unordered_map frequency pattern from this lesson, graded.
Sum of Array Elements ↗ accumulate with a 0LL start — try it both by loop and by algorithm.
Reverse an array ↗ reverse() in one line — then write the manual two-pointer version to understand it.
Basic Hashing The concept lesson behind the map: hashing from first principles.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.