C++ Cheat Sheet
Modern C++ syntax covering pointers, references, STL containers, templates, smart pointers, and RAII memory management.
2 PagesIntermediateApr 12, 2026
Basic Syntax
Variables, control flow, and I/O.
cpp
#include <iostream>using namespace std;int main() { int age = 30; // stack-allocated int double pi = 3.14159; string name = "Ada"; // std::string if (age >= 18) { cout << name << " is an adult" << endl; } for (int i = 0; i < 5; i++) { cout << "Count: " << i << endl; } return 0;}
STL Containers
Vectors, maps, and algorithms.
cpp
#include <vector>#include <map>#include <algorithm>vector<int> nums = {5, 3, 1, 4, 2};sort(nums.begin(), nums.end()); // {1, 2, 3, 4, 5}nums.push_back(6);nums.size(); // 6map<string, int> ages;ages["Alice"] = 30;ages.count("Alice"); // 1for (auto& [name, age] : ages) { // structured bindings (C++17) cout << name << ": " << age << endl;}
Smart Pointers & RAII
Automatic memory management.
cpp
#include <memory>unique_ptr<int> up = make_unique<int>(42); // sole owner, auto-deletedshared_ptr<int> sp = make_shared<int>(10); // reference-countedshared_ptr<int> sp2 = sp; // ref count = 2// avoid raw `new`/`delete` in modern C++class Widget {public: Widget() { cout << "created" << endl; } ~Widget() { cout << "destroyed" << endl; } // RAII cleanup};
Core Keywords
Common modern C++ keywords.
- const- declares a value or method that cannot modify state
- constexpr- evaluated at compile time when possible
- virtual- enables runtime polymorphism via vtable dispatch
- override- marks a method as overriding a virtual base method
- namespace- groups identifiers to avoid naming collisions
- auto- lets the compiler deduce the variable's type
- template<typename T>- defines generic functions/classes parameterized by type
- nullptr- type-safe null pointer constant (replaces NULL/0)
Move Semantics
Transfer resources cheaply with rvalue references and std::move.
cpp
class Buffer { int* data; size_t n;public: Buffer(Buffer&& o) noexcept : data(o.data), n(o.n) { o.data = nullptr; o.n = 0; // steal, leave source empty } Buffer& operator=(Buffer&& o) noexcept { if (this != &o) { delete[] data; data = o.data; n = o.n; o.data = nullptr; o.n = 0; } return *this; }};Buffer b = std::move(other); // invokes move ctor
Templates & Concepts
Generic code with C++20 constrained templates.
cpp
#include <concepts>template <typename T>concept Numeric = std::integral<T> || std::floating_point<T>;template <Numeric T>T add(T a, T b) { return a + b; }// Variadic templatetemplate <typename... Args>auto sum(Args... args) { return (args + ...); } // fold expressionauto s = sum(1, 2, 3, 4); // 10
Lambdas & Algorithms
Closures with capture lists used with STL algorithms.
cpp
#include <algorithm>#include <vector>std::vector<int> v{5, 2, 8, 1, 9};int threshold = 4;// capture threshold by value, sort descendingstd::sort(v.begin(), v.end(), [](int a, int b){ return a > b; });auto count = std::count_if(v.begin(), v.end(), [threshold](int x){ return x > threshold; });// mutable lambda keeps its own stateauto counter = [i = 0]() mutable { return ++i; };
References & Value Categories
Reference types and expression categories.
- T&- lvalue reference; binds to a named, addressable object
- T&&- rvalue reference; binds to temporaries, enables moves
- const T&- read-only reference; binds to both lvalues and rvalues
- std::move(x)- casts x to an rvalue so its resources can be stolen
- std::forward<T>(x)- perfect-forwards preserving the original value category
- auto&&- forwarding reference in a template/auto context
Pro Tip
Follow the Rule of Five: if you define a destructor, copy constructor, copy assignment, move constructor, or move assignment, you likely need to define all five.
Was this cheat sheet helpful?
Explore Topics
#CCheatSheet#Programming#Intermediate#BasicSyntax#STLContainers#SmartPointersRAII#CoreKeywords#Docker#CheatSheet#SkillVeris