C++ Cheat Sheet

A quick reference of the most used C++ syntax. Every example was compiled with GCC 16 and -std=c++23.

Program, variables and types

#include <iostream>
#include <string>

int main() {
    int n = 5;
    double d{3.14};
    char c = 'A';
    bool ok = true;
    std::string s = "text";
    auto x = 10;                       // int
    const int MAX = 100;

    std::cout << n << ' ' << d << ' ' << c << ' ' << ok << ' ' << s << ' ' << x << ' ' << MAX << '\n';
}
Output

5 3.14 A 1 text 10 100

Control flow

#include <iostream>

int main() {
    int n = 7;
    if (n > 5) std::cout << "big\n"; else std::cout << "small\n";

    switch (n % 3) {
        case 0: std::cout << "zero\n"; break;
        case 1: std::cout << "one\n"; break;
        default: std::cout << "two\n";
    }

    for (int i = 0; i < 3; i++) std::cout << i << ' ';
    int k = 0;
    while (k < 2) { std::cout << 'w' << k << ' '; k++; }
    std::cout << '\n';
}
Output

big
one
0 1 2 w0 w1

Containers and algorithms

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

int main() {
    std::vector<int> v = {3, 1, 2};
    v.push_back(0);
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';

    std::map<std::string, int> m{{"a", 1}};
    m["b"] = 2;
    for (const auto& [key, value] : m) std::cout << key << value << ' ';
    std::cout << '\n';
}
Output

0 1 2 3
a1 b2

Functions, references and pointers

SyntaxMeaning
int f(int a)Pass by value (a copy)
int f(int& a)Pass by reference (can change it)
int f(const std::string& s)Pass without copy, read only
int* p = &x;Pointer to x
*pThe value at the pointer
auto p = std::make_unique<T>()Owning smart pointer

Classes

#include <iostream>
#include <memory>
#include <string>

class Animal {
public:
    explicit Animal(std::string name) : name(std::move(name)) {}
    virtual ~Animal() = default;
    virtual std::string speak() const = 0;
protected:
    std::string name;
};

class Dog : public Animal {
public:
    using Animal::Animal;
    std::string speak() const override { return name + " says Woof"; }
};

int main() {
    std::unique_ptr<Animal> a = std::make_unique<Dog>("Tommy");
    std::cout << a->speak() << '\n';
}
Output

Tommy says Woof

Templates and lambdas

#include <algorithm>
#include <iostream>
#include <vector>

template <typename T>
T maximum(T a, T b) { return a > b ? a : b; }

int main() {
    std::cout << maximum(3, 9) << '\n';
    std::vector<int> v = {5, 2, 9};
    std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
    std::cout << v.front() << '\n';
}
Output

9
9

Exceptions and files

#include <fstream>
#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("problem");
    } catch (const std::exception& e) {
        std::cout << e.what() << '\n';
    }
    std::ofstream("cheat.txt") << "saved";
    std::cout << "done\n";
}
Output

problem
done

Compile commands

g++ -std=c++23 -Wall -Wextra main.cpp -o main      # compile
./main                                             # run
g++ -std=c++23 -O2 -g main.cpp -o main             # optimized, with debug info
g++ -std=c++23 -fsanitize=address,undefined main.cpp -o main   # find memory bugs

Where to go next

  • Practice with the C++ exercises and interview questions of this website.
  • Learn a build system (CMake) and a testing library (Catch2 or GoogleTest).
  • Build small projects: a text game, a bank system, a to do list in the terminal.
Note: Compile with warnings on, and read every warning. Revise one section every day.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.