All cheatsheets

Cheatsheets

C++

Modern C++: RAII, smart pointers, move semantics, the STL, templates, and UB pitfalls.

Test yourself

54 entries

Variables & types7

int / double / bool / char

Fundamental types

std::int32_t / std::size_t

Fixed-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 constexpr

const = 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& x

Read-only reference — cheap, can't mutate or be null

T&& x

Rvalue reference — binds to temporaries (move semantics)

nullptr

Null 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

RAII

Tie 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) noexcept

Move constructor — steal other's resources

T& operator=(T&&) noexcept

Move assignment operator

std::forward<T>(arg)

Perfect-forward in templates (preserve value category)

Rule of 0/3/5

Manage 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_map

Ordered tree (O(log n), sorted) vs hash table (O(1) average, unordered).

std::array<T, N>

Fixed-size stack array with STL interface

std::string

Owning, growable text — see the String group

std::set<T> / std::unordered_set<T>

Unique keys (sorted / hashed)

std::pair / std::tuple

Group 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 / accumulate

Prefer 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_each

Map a function over a range

std::count_if / any_of / all_of

Count / 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::string

Owning, 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-correctness

Mark everything that doesn't mutate as const — the compiler then enforces it.

std::string_view

Non-owning read-only view of a string (no copy)

s.find("x") != std::string::npos

Search; npos = not found

std::variant<int, std::string>

Type-safe tagged union (C++17)

Common UB & pitfalls6

Dangling reference / iterator

Using a reference, pointer, or iterator after the thing it points to is gone — undefined behavior.

Out-of-bounds / signed overflow

Reading past an array or overflowing a signed int is undefined — not a guaranteed crash.

Use-after-move

Reading a moved-from object — value is unspecified

Uninitialized variable

Reading an uninitialized local is UB — always initialize

= vs == in conditions

if (x = 5) assigns; compile with -Wall to catch

Slicing

Copying a derived object into a base by value drops the derived parts