C++ Cheat Sheet
A dense, scannable quick reference for syntax you already know from the rest of this tutorial. Use it to jog your memory, not to learn a topic from scratch — follow the sidebar links for the full explanations.
Data Types
CPP
bool b = true; char c = 'A'; int i = 42; unsigned int u = 42u; long l = 42L; long long ll = 42LL; float f = 3.14f; double d = 3.14159; std::string s = "hello"; auto x = 3.14; // deduced as double
Operators
CPP
// Arithmetic a + b a - b a * b a / b a % b // Comparison a == b a != b a < b a > b a <= b a >= b // Logical a && b a || b !a // Bitwise a & b a | b a ^ b ~a a << n a >> n // Assignment a += b a -= b a *= b a /= b a %= b // Increment / decrement ++a a++ --a a-- // Ternary int max = (a > b) ? a : b;
Control Flow
CPP
if (cond) { } else if (other) { } else { }
switch (value) {
case 1: /* ... */ break;
default: /* ... */
}
for (int i = 0; i < n; ++i) { }
for (auto& item : container) { }
while (cond) { }
do { } while (cond);
break; // exit loop/switch
continue; // skip to next iterationFunctions
CPP
int add(int a, int b) { return a + b; }
// Default argument
void greet(std::string name = "World") { }
// Pass by reference (no copy, can modify)
void increment(int& x) { ++x; }
// Pass by const reference (no copy, read-only)
void print(const std::string& s) { }
// Lambda
auto square = [](int x) { return x * x; };
auto captureByRef = [&total](int x) { total += x; };Classes
CPP
class Animal {
public:
Animal(std::string name) : name_(name) {}
virtual ~Animal() = default;
virtual void speak() const { std::cout << name_ << " makes a sound\n"; }
std::string getName() const { return name_; }
private:
std::string name_;
};
class Dog : public Animal {
public:
using Animal::Animal;
void speak() const override { std::cout << getName() << " barks\n"; }
};STL Containers
CPP
std::vector<int> v = {1, 2, 3}; // dynamic array
std::array<int, 3> a = {1, 2, 3}; // fixed-size array
std::list<int> l = {1, 2, 3}; // doubly linked list
std::deque<int> dq; // double-ended queue
std::set<int> s; // sorted unique elements
std::unordered_set<int> us; // hash-based, unordered
std::map<std::string, int> m; // sorted key-value pairs
std::unordered_map<std::string, int> um; // hash-based key-value pairs
std::stack<int> st; // LIFO
std::queue<int> q; // FIFO
std::pair<int, std::string> p = {1, "a"};
std::tuple<int, std::string, double> t = {1, "a", 2.5};Common Algorithms (<algorithm>)
CPP
std::sort(v.begin(), v.end());
std::sort(v.begin(), v.end(), std::greater<int>());
std::find(v.begin(), v.end(), 42);
std::count(v.begin(), v.end(), 42);
std::max_element(v.begin(), v.end());
std::min_element(v.begin(), v.end());
std::reverse(v.begin(), v.end());
std::accumulate(v.begin(), v.end(), 0); // <numeric>
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x; });
std::all_of(v.begin(), v.end(), [](int x) { return x > 0; });
std::transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; });