C++ Cheat Sheet
Last updated
Hello World & program structure
Execution starts at main, which returns an int.
| Element | Code |
|---|---|
| Include a header | #include <iostream> |
| Entry point | int main() { ... } |
| Print a line | std::cout << "Hello, World!" << std::endl; |
| Read input | std::cin >> x; |
| Use the std namespace | using namespace std; |
| Return success | return 0; |
| Comments | // line and /* block */ |
Data types
| Type | Description |
|---|---|
int | Integer (usually 32 bits) |
long / long long | Wider integers |
float / double | Floating point numbers |
char | Single byte / character |
bool | true or false |
std::string | Dynamic text from <string> |
auto | Compiler infers the type |
std::size_t | Unsigned type for sizes and indices |
Variables & references
References are aliases; pointers hold addresses.
| Operation | Syntax |
|---|---|
| Declare & initialize | int x = 5; |
| Brace initialization | int x{5}; |
| Constant | const double PI = 3.14159; |
| Compile-time constant | constexpr int N = 10; |
| Reference (alias) | int &r = x; |
| Pointer | int *p = &x; |
| Dereference a pointer | *p = 10; |
| Type inference | auto y = 3.14; |
Control flow
| Statement | Syntax |
|---|---|
| If / else | if (x > 0) { ... } else { ... } |
| Switch | switch (n) { case 1: ...; break; default: ...; } |
| While loop | while (i < n) { ... } |
| Do-while loop | do { ... } while (i < n); |
| For loop | for (int i = 0; i < n; i++) { ... } |
| Range-based for | for (auto x : vec) { ... } |
| Range-based by reference | for (auto &x : vec) { ... } |
| Break / continue | break; exits a loop, continue; skips to next iteration |
Functions
| Operation | Syntax |
|---|---|
| Define a function | int add(int a, int b) { return a + b; } |
| No return value | void greet() { ... } |
| Default arguments | int pow(int b, int e = 2) { ... } |
| Pass by reference | void inc(int &x) { x++; } |
| Const reference (no copy) | void print(const std::string &s) { ... } |
| Overloading | int max(int a, int b); and double max(double a, double b); |
| Lambda | auto f = [](int x) { return x * 2; }; |
| Lambda with capture | auto g = [n](int x) { return x + n; }; |
Classes & OOP
| Operation | Syntax |
|---|---|
| Define a class | class Dog { ... }; |
| Access specifiers | public:, private:, protected: |
| Member variable | std::string name; |
| Constructor | Dog(std::string n) : name(n) {} |
| Destructor | ~Dog() { ... } |
| Member function | void bark() { ... } |
| Create an object | Dog d("Rex"); |
| Inheritance | class Puppy : public Dog { ... }; |
| Virtual function | virtual void speak(); |
STL containers
Common containers from the standard library.
| Container | Use & example |
|---|---|
std::vector<int> | Dynamic array: v.push_back(1); v.size(); |
std::string | Text: s += "!"; s.length(); |
std::array<int, 3> | Fixed-size array with bounds info |
std::map<K, V> | Sorted key-value: m["a"] = 1; |
std::unordered_map<K, V> | Hash map (faster lookup, no order) |
std::set<T> | Sorted unique values: s.insert(5); |
std::pair<A, B> | Two values: make_pair(1, "x") |
std::queue / std::stack | FIFO queue / LIFO stack adapters |
STL algorithms & iterators
From <algorithm>; most take a begin/end iterator range.
| Operation | Syntax |
|---|---|
| Begin / end iterators | v.begin(), v.end() |
| Sort | std::sort(v.begin(), v.end()); |
| Sort descending | std::sort(v.begin(), v.end(), std::greater<int>()); |
| Find a value | auto it = std::find(v.begin(), v.end(), 5); |
| Count matches | std::count(v.begin(), v.end(), 5); |
| Min / max element | std::max_element(v.begin(), v.end()); |
| Sum a range | std::accumulate(v.begin(), v.end(), 0); |
| Transform each element | std::transform(v.begin(), v.end(), v.begin(), fn); |
Smart pointers & modern C++
Prefer smart pointers over raw new/delete for automatic cleanup.
| Feature | Syntax |
|---|---|
| Unique ownership | auto p = std::make_unique<Dog>("Rex"); |
| Shared ownership | auto p = std::make_shared<Dog>("Rex"); |
| Non-owning reference | std::weak_ptr<Dog> w = p; |
| Move semantics | auto v2 = std::move(v1); |
| Nullptr | int *p = nullptr; |
| Structured bindings | auto [key, val] = *it; |
| Optional value | std::optional<int> maybe; |
| Templates | template <typename T> T add(T a, T b) { return a + b; } |
The C++ syntax, STL containers, and modern features you reach for most, on one page. This C++ cheat sheet is a quick reference for writing C++ - the data types, references, classes, the standard library containers like vector and unordered_map, and the smart pointers that replace raw new/delete.
Everything here is standard C++ (C++11 and later) and compiles with g++ or clang++. Copy what you need, or try any snippet live in the C++ playground - no compiler to install. Learning the language? Coddy's free interactive C++ course covers pointers, the STL, and classes step by step.
C++ cheat sheet FAQ
Is this C++ cheat sheet free?
Yes. This C++ cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up syntax, an STL container, or a smart pointer.
What is the difference between a pointer and a reference in C++?
*p. A reference (int &r = x) is an alias for an existing variable - it must be bound when declared, can never be null or rebound, and is used like the variable itself. Use references for cleaner pass-by-reference parameters and pointers when you need optional or reassignable indirection.Should I use new/delete or smart pointers?
std::unique_ptr and std::shared_ptr (via make_unique/make_shared) free their memory automatically when they go out of scope, which prevents the leaks and double-frees that come with manual new/delete. Reach for raw new/delete only in low-level code that has a specific reason.Can I practice C++ online?
Yes. Open the C++ playground to compile and run any snippet from this cheat sheet in your browser - no compiler to install. When you want structure, Coddy's free interactive C++ course takes you from variables and loops to classes and the STL step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the most common building blocks (types, control flow, functions) down to advanced ones (the STL and smart pointers), so you can use the top sections on day one and grow into the rest.