Language cheatsheet
C++ 23 Cheatsheet
Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
Build and debug
- Compile a program — Build main.cpp into an executable with C++23, warnings and optimization on.
- Run the program — Run the executable you just built from the current folder.
- Compile several source files — Compile and link more than one source file into one program.
- Compile to an object file — Compile one file without linking, which speeds up rebuilds of large projects.
- Build for debugging — Add debug symbols and disable optimization so a debugger can step through the code.
- Find memory bugs with sanitizers — Detect out-of-bounds access, use-after-free and undefined behavior at runtime.
- Debug with gdb — Start the debugger, then use break main, run, next, step, print and backtrace.
- Check for memory leaks — Run a program under Valgrind and report leaks in detail.
- Format the code — Reformat source files in place using the .clang-format style.
- Lint the code — Run static analysis and style checks on a file.
CMake
- A minimal CMakeLists.txt — A project with C++23 and one executable target.
- Configure a build — Generate the build system in a separate build folder. Release mode turns on optimization.
- Build the project — Compile using the generated build system, in parallel.
- Run the tests — Run the tests registered with add_test and print output for failures.
- Export compile commands — Write compile_commands.json, which editors and clang-tidy use.
- Link a library — Link a target against another target or an installed package.
- Download a dependency — Fetch and build a dependency such as GoogleTest at configure time.
Language basics
- Hello world — The smallest C++ program, printing a line with iostream.
- Loop over a container — Use auto and a range-based for loop. const auto& avoids copying each element.
- Unpack a pair or struct — Name the parts of a pair, tuple or struct in one declaration.
- Format text — Build a string with placeholders using std::format (C++20).
- Pass a string without copying — Take a std::string_view parameter to accept strings and literals cheaply.
- Define a scoped enum — A strongly typed enum whose names don't leak into the surrounding scope.
- Compute at compile time — A constexpr function can be evaluated while compiling, and static_assert checks it.
- Declare a variable in an if — Limit a variable's scope to the if and else branches.
- Warn when a result is ignored — Mark a function whose return value should be used.
Containers and algorithms
- Use a vector — A growable array. emplace_back constructs the element in place.
- Use a hash map — Look up values by key. contains is available since C++20.
- Sort with a custom order — Sort descending by passing a lambda comparator.
- Find the first match — Search with a predicate. The result is an iterator, end() if nothing matches.
- Sum a range — Add up the elements, starting from 0.
- Remove matching elements — Erase every element that satisfies a condition (C++20).
- Sort with ranges — Pass the container directly instead of a begin and end pair (C++20).
- Chain lazy views — Filter and transform without creating intermediate containers (C++20).
- Represent a missing value — std::optional holds a value or nothing, instead of a sentinel like -1.
- Hold one of several types — A type-safe union, visited with std::visit.
- View a contiguous range — A non-owning view over an array or vector (C++20).
Classes, memory and templates
- Define a class — A class with a constructor initializer list, a const method and private data.
- Use polymorphism — Override a virtual function in a derived class. Always give a base class a virtual destructor.
- Own an object with unique_ptr — Allocate an object that is deleted automatically when the pointer goes out of scope. Avoid raw new and delete.
- Share ownership with shared_ptr — The object is deleted when the last shared_ptr to it goes away.
- Move instead of copy — Transfer a resource to another object without copying it.
- Lock a mutex safely — RAII locking releases the mutex even if an exception is thrown.
- Write a function template — One function that works for any type that supports >.
- Constrain a template with a concept — Accept only integral types and get clear error messages otherwise (C++20).
- Capture variables in a lambda — Capture by value with [=] or by reference with [&], or name what you need.
- Return a value or an error — std::expected holds a result or an error without exceptions (C++23).
Input, exceptions and threads
- Read a file line by line — Read a text file with ifstream and getline.
- Read a line from the keyboard — Read a whole line, including spaces.
- Handle exceptions — Catch exceptions by const reference.
- Run code on another thread — Start a thread and wait for it. std::jthread joins automatically when it is destroyed (C++20).
- Compute a value in the background — Run a function asynchronously and collect the result later.
- Share a counter between threads — An atomic integer can be updated by several threads without a mutex.
- Time a section of code — Measure elapsed time with a steady clock.
Related cheatsheets
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.
- JavaScript 2025 cheatsheet — Modern JavaScript for arrays, strings, objects, async code, the DOM and modules, in copy-ready snippets.