When to use which part of the C++ standard library, what every operation costs and invalidates, and what the common contest libraries provide. Language-level pitfalls (overflow, comparators, hashing) are in C++ for Competitive Programming; data structures beyond the STL are in the Data Structure Catalog.

Choosing a container

NeedUseNot
a sequence, random access, appendvectorlist (cache misses, 24+ bytes per element)
push / pop at both endsdeque, or a ring buffer on a vectorvector::insert(begin()) ()
LIFO / FIFO onlyvector as a stack, queue or a head index on a vector
ordered keys, predecessor / successor, min / max under updatesset / mapunordered_* (no order)
duplicates with ordermultiset (erase one copy via erase(find(x)))multiset::count in a loop ()
exact lookup only, hot loopunordered_map with a randomized hash and reserve, or gp_hash_tabledefault hash<long long> (anti-hash tests)
keys in a plain array or vectorany map
-th element / rank in a dynamic setpb_ds tree, or a Fenwick tree over compressed valuesstd::distance on set iterators ()
max / min with insertionspriority_queue (max-heap by default)set when no erase-arbitrary is needed (3x slower)
booleans in bulk, word-parallel operationsbitset<N> (compile-time ), vector<char> for plain flagsvector<bool> when you need references
stringsstring, string_view for read-only sliceschar* arithmetic
iterator stability while insertinglist, set, mapvector, deque

Containers: costs and invalidation

ContainerAccessInsertEraseFindInvalidated by insertInvalidated by erase
vectorback , else back , else all if reallocation, else from the insertion point onfrom the erased element on
dequeends , else ends , else all iterators (references at the ends stay valid)ends: only erased; middle: all
list, forward_list at an iteratornoneonly the erased
set, map, multi*— (hinted: ), by iterator noneonly the erased
unordered_*—all iterators on rehash (references stay valid)only the erased
priority_queuetop top ———
stringback back find like vectorlike vector
bitset<N>——_Find_first, _Find_next ——

amortized, expected. Measured memory per element is in the Data Structure Catalog (vector<int> 4 B, set<int> 40 B, unordered_map<int,int> 27 B, plus allocator overhead for node containers).

Algorithms

AlgorithmCostNotes
sort worst (introsort)not stable
stable_sort with a buffer, withoutmerge sort
partial_sort(b, b + k, e)smallest , sorted
nth_element(b, b + k, e) average-th smallest in place, smaller ones before it
lower_bound, upper_bound, equal_range, binary_search comparisonson random-access ranges; on set use the member functions
partition_point(b, e, pred)binary search on a predicate: Binary Search
unique + eraseremoves adjacent duplicates: sort first
next_permutation / prev_permutation worst, amortized over all permutationsreturns false after the last one; start from sorted
reverse, rotate, shuffleshuffle(all(a), rng) with mt19937, never random_shuffle
merge, inplace_merge ( in place without a buffer)merge-sort steps, CDQ
set_union, set_intersection, set_difference, includessorted inputs
accumulate(b, e, 0LL)the type of the initial value is the type of the sum: 0 overflows
partial_sum, adjacent_difference, inclusive_scanprefix sums and differences
iota, fill, count, count_if, find_if, all_of, any_of
min_element, max_element, minmax_elementreturn iterators
gcd, lcm (<numeric>)lcm can overflow; divide first
__builtin_popcountll, __builtin_clzll, __builtin_ctzll, __lgclz(0), ctz(0) are undefined
C++20 <bit>: popcount, countl_zero, countr_zero, bit_width, bit_ceil, has_single_bitunsigned types only
C++20 ranges: ranges::sort(a), views::iota, views::reversesame as aboveshorter code; std::span for array views
vector<int> compressRanks(const vector<ll>& a) {            // rank of each value among the distinct values
    vector<ll> v(a); sort(all(v)); v.erase(unique(all(v)), v.end());
    vector<int> r(sz(a));
    for (int i = 0; i < sz(a); i++) r[i] = int(lower_bound(all(v), a[i]) - v.begin());
    return r;
}
 
ll countInRange(const vector<ll>& sorted, ll lo, ll hi) {   // how many values lie in [lo, hi]
    return upper_bound(all(sorted), hi) - lower_bound(all(sorted), lo);
}
 
vector<int> argsortStable(const vector<ll>& a) {           // indices by value, ties by index
    vector<int> p(sz(a)); iota(all(p), 0);
    stable_sort(all(p), [&](int i, int j) { return a[i] < a[j]; });
    return p;
}
 
ll kthSmallest(vector<ll> a, int k) {                      // 0-based k
    nth_element(a.begin(), a.begin() + k, a.end());
    return a[k];
}
 
vector<ll> sortedIntersection(const vector<ll>& a, const vector<ll>& b) {   // a, b sorted
    vector<ll> c; set_intersection(all(a), all(b), back_inserter(c));
    return c;
}

Policy-based data structures (__gnu_pbds)

StructureDeclarationExtra operations
order-statistics treetree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>find_by_order(k) (-th, 0-based), order_of_key(x) (count ),
multiset versionkey pair<T, int> with a unique counter, or less_equal<T> (breaks find / erase)
hash tablegp_hash_table<K, V, Hash> (open addressing), cc_hash_table (chaining)usually 2–3x faster than unordered_map; still needs a randomized hash
priority queue__gnu_pbds::priority_queue<T, Cmp, pairing_heap_tag>join (meld), modify (decrease-key) through point iterators
rope (<ext/rope>)__gnu_cxx::rope<T> insert / erase / substring; persistence by copy

Details and examples: Ordered Set and PBDS. pb_ds is a GCC extension: it does not exist on MSVC or Clang with libc++.

Contest libraries and what each is good for

LibraryContentsBest for
KACTL (KTH team notebook)contest (template, hash, stress-test scripts), mathematics, data-structures (OrderStatisticTree, HashMap, SegmentTree, LazySegmentTree, Treap, LineContainer, RMQ, MoQueries), numerical (FFT, NTT, FFT mod, polynomial roots, Simplex, Gaussian elimination, determinant), number-theory (Miller–Rabin, Pollard rho, ModSqrt, CRT, phi), combinatorial, graph (Dinic, push-relabel, min-cost flow, Hungarian, general matching, SCC, 2-SAT, link-cut tree, HLD, dominator tree), geometry (Point, convex hull, polygon tools, 3D hull, Delaunay), strings (KMP, Z, Manacher, suffix array, suffix tree, Aho–Corasick, hashing), variousICPC: fits the 25-page reference limit, every file is short and tested; adapt by removing what the team never uses and adding the team’s own snippets
AtCoder Library (ACL)modint, convolution (NTT, convolution_ll), fenwick_tree, segtree, lazy_segtree, dsu, maxflow, mcf_graph, scc_graph, two_sat, string (suffix_array, lcp_array, z_algorithm), math (pow_mod, inv_mod, crt, floor_sum)AtCoder (preinstalled); a clean, well-tested API to copy into Codeforces solutions
Library Checker (yosupo)a judge with one problem per algorithm (convolution, polynomial ops, matching, …)verifying your own templates
ei1333 library, Nyaan’s libraryvery broad, heavily verified libraries (polynomials, graphs, data structures)looking up an implementation of an advanced algorithm
cp-algorithms (e-maxx)articles with code for most classical algorithmslearning an algorithm with proofs
the owner’s templatethe personal template repositorysee 🐬 Templates

Rules of thumb: know your library’s interfaces before the contest; never paste code you cannot debug; for ICPC, keep the notebook organised by chapter and include the complexity and pitfalls of every snippet.