Cheatsheets
C++
Modern C++: RAII, smart pointers, move semantics, the STL, templates, and UB pitfalls.
Test yourself54 entries
Variables & types7
int / double / bool / charFundamental types
std::int32_t / std::size_tFixed-width / size types (<cstdint>)
auto x = expr;Deduce the type from the initializer — less noise, no implicit conversions.
constexpr int N = 8;Compile-time constant / function
using ID = std::int64_t;Type alias (prefer over typedef)
const vs constexprconst = won't change at runtime; constexpr = known at compile time.
enum class Color { Red, Green };Scoped enum — no implicit int conversion
References & pointers5
T& ref = obj; vs T* ptr = &obj;Reference = an alias that can't be null or rebound; pointer = a nullable address.
const T& xRead-only reference — cheap, can't mutate or be null
T&& xRvalue reference — binds to temporaries (move semantics)
nullptrNull pointer constant — use instead of NULL or 0
Raw new / delete (avoid)Manual heap management — error-prone; reach for smart pointers instead.
RAII & smart pointers7
RAIITie a resource's lifetime to an object's scope — acquire in the constructor, release in the destructor.
std::unique_ptr<T>Exclusive ownership — non-copyable, moves only, zero overhead vs raw pointer.
std::shared_ptr<T>Shared ownership via reference counting — object freed when the last owner dies.
std::make_unique<T>(args)Construct a unique_ptr (no naked new)
std::make_shared<T>(args)Construct a shared_ptr (one allocation)
std::weak_ptr<T>Non-owning observer; .lock() to use; breaks cycles
p.get()Borrow the raw pointer without transferring ownership
Move semantics5
std::move(x)Cast to an rvalue so its resources can be stolen instead of copied — does NOT itself move.
T(T&& other) noexceptMove constructor — steal other's resources
T& operator=(T&&) noexceptMove assignment operator
std::forward<T>(arg)Perfect-forward in templates (preserve value category)
Rule of 0/3/5Manage all special members consistently — or, ideally, none of them.
STL containers7
std::vector<T>Dynamic contiguous array — the default container for a sequence.
std::map vs std::unordered_mapOrdered tree (O(log n), sorted) vs hash table (O(1) average, unordered).
std::array<T, N>Fixed-size stack array with STL interface
std::stringOwning, growable text — see the String group
std::set<T> / std::unordered_set<T>Unique keys (sorted / hashed)
std::pair / std::tupleGroup 2 / N heterogeneous values
std::deque<T> / std::list<T>Double-ended queue / doubly linked list
Iterators, range-for & algorithms6
for (const auto& e : c)Range-based for — iterate any container without indices or iterators.
std::sort / find / accumulatePrefer standard algorithms over hand-written loops — correct, optimized, expressive.
v.begin() / v.end()Iterator range [begin, end) — end is one-past-last
std::ranges::sort(v)C++20 ranges — pass the container directly, no begin/end
std::transform / std::for_eachMap a function over a range
std::count_if / any_of / all_ofCount / test elements with a predicate
Templates & lambdas5
template <typename T>Generic code instantiated per type at compile time — type-safe and zero-overhead.
[](int x){ return x*2; }Lambda — an inline anonymous function, optionally capturing local state.
auto f = [](auto x){ return x; };Generic lambda (auto params, C++14)
std::function<int(int)>Type-erased callable wrapper (has overhead)
template <auto N>Non-type template parameter (value, C++17)
std::string, optional & const-correctness6
std::stringOwning, growable string — concatenation, search, and conversion built in.
std::optional<T>A value that may or may not be present — type-safe 'maybe' without sentinels or pointers.
const-correctnessMark everything that doesn't mutate as const — the compiler then enforces it.
std::string_viewNon-owning read-only view of a string (no copy)
s.find("x") != std::string::nposSearch; npos = not found
std::variant<int, std::string>Type-safe tagged union (C++17)
Common UB & pitfalls6
Dangling reference / iteratorUsing a reference, pointer, or iterator after the thing it points to is gone — undefined behavior.
Out-of-bounds / signed overflowReading past an array or overflowing a signed int is undefined — not a guaranteed crash.
Use-after-moveReading a moved-from object — value is unspecified
Uninitialized variableReading an uninitialized local is UB — always initialize
= vs == in conditionsif (x = 5) assigns; compile with -Wall to catch
SlicingCopying a derived object into a base by value drops the derived parts