C++ STL
cpp-stl
Code Snippets
std::vector Quick Reference
`std::vector` is the default sequence container in C++: a contiguous, dynamically resizing array. This snippet shows the core operations (`push_back`, `emplace_back`, indexed access, range iteration) plus reservation patterns to avoid reallocation churn. Reach for it whenever you would reach for an `ArrayList` in Java or a list literal in Python.
std::unordered_map Quick Reference
`std::unordered_map` is the default hash table in C++: average O(1) insert, lookup, and erase. This snippet covers insertion, the right way to test for membership without inserting a default, iteration, and structured bindings (C++17) for clean key/value loops. Use it whenever you need a dictionary; reach for `std::map` only when you need keys in sorted order.
Range-Based For Loop Patterns
The range-based `for` loop (C++11) is the idiomatic way to iterate any container or any type with `begin()` / `end()`. This snippet covers the three reference flavours (by value, by const reference, by mutable reference), iterating over arrays and `std::initializer_list`, and how to also access the index when you need it. Get the reference forms right and you avoid both copies and accidental mutations.
C++ Lambda Basics
Lambdas (C++11+) are first-class anonymous function objects with a compact syntax: `[capture](params) { body }`. This snippet covers the basic form, the difference between by-value `[=]` and by-reference `[&]` captures, and using lambdas with standard algorithms like `std::sort` and `std::for_each`. Reach for them anywhere you would write a small functor or pass a callback.
Min-Heap via std::priority_queue
`std::priority_queue` is a max-heap by default. To get a min-heap you flip the comparator. This snippet shows the three common forms: a min-heap of ints with `std::greater`, a min-heap of pairs sorting by first element, and a custom comparator over a struct for things like Dijkstra. All run in O(log n) per push/pop.
