C++ Cheat Sheet
C++ is a general-purpose programming language that combines low-level memory control with high-level abstractions. This cheatsheet provides a structured, example-driven reference for core C++ syntax, memory handling, object-oriented features, templates, and the Standard Template Library (STL).
Compilation
g++ main.cpp -o app
./app
g++ -std=c++20 main.cpp -o app
g++ main.cpp utils.cpp -o app
Minimal Program
#include <iostream>
int main() {
std::cout << "Hello, C++" << std::endl;
return 0;
}
Fundamental Types
int a = 10;
double b = 3.14;
char c = 'A';
bool d = true;
sizeof(int);
sizeof(double);
sizeof(char);
Initialization
int x = 10;
int y{20};
auto z = 30;
References
int value = 10;
int& ref = value;
ref = 20;
Pointers
int value = 10;
int* ptr = &value;
*ptr = 30;
int** pptr = &ptr;
Arrays
int arr[3] = {1, 2, 3};
arr[0] = 10;
std::array
#include <array>
std::array<int, 3> arr = {1, 2, 3};
arr.at(0);
arr.size();
Control Flow
if (x > 10) {
result = 1;
} else {
result = 0;
}
switch (x) {
case 1:
break;
default:
break;
}
for (int i = 0; i < 5; i++) {
std::cout << i << std::endl;
}
Functions
int add(int a, int b) {
return a + b;
}
int result = add(2, 3);
Function Overloading
int add(int a, int b);
double add(double a, double b);
Default Arguments
int multiply(int a, int b = 2) {
return a * b;
}
Structs
struct Point {
int x;
int y;
};
Point p{10, 20};
Classes
class Counter {
int value;
public:
Counter() : value(0) {}
void increment() { value++; }
int get() const { return value; }
};
Constructors & Destructors
class Resource {
public:
Resource() {}
~Resource() {}
};
Inheritance
class Animal {
public:
void speak() {}
};
class Dog : public Animal {
};
Virtual Functions
class Base {
public:
virtual void run() {}
};
class Derived : public Base {
public:
void run() override {}
};
Dynamic Memory
int* p = new int(10);
delete p;
int* arr = new int[10];
delete[] arr;
Smart Pointers
#include <memory>
std::unique_ptr<int> p1 = std::make_unique<int>(10);
std::shared_ptr<int> p2 = std::make_shared<int>(20);
Templates
template <typename T>
T add(T a, T b) {
return a + b;
}
add<int>(2, 3);
add<double>(1.1, 2.2);
STL Containers
std::vector
#include <vector>
std::vector<int> v = {1, 2, 3};
v.push_back(4);
v[0];
std::map
#include <map>
std::map<int, int> m;
m[1] = 100;
std::unordered_map
#include <unordered_map>
std::unordered_map<int, int> um;
Iteration
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << std::endl;
}
for (const auto& item : v) {
std::cout << item << std::endl;
}
Algorithms
#include <algorithm>
std::sort(v.begin(), v.end());
std::find(v.begin(), v.end(), 3);
Lambda Expressions
auto square = [](int x) {
return x * x;
};
square(5);
Namespaces
namespace math {
int add(int a, int b) {
return a + b;
}
}
math::add(2, 3);
Exceptions
try {
throw 1;
} catch (int e) {
}
Type Casting
double x = 3.14;
int y = static_cast<int>(x);
File I/O
#include <fstream>
std::ofstream out("file.txt");
out << "text";
out.close();
std::ifstream in("file.txt");
Move Semantics
#include <vector>
std::vector<int> create() {
std::vector<int> v = {1, 2, 3};
return v;
}
RAII Example
#include <fstream>
class File {
std::fstream f;
public:
File(const std::string& name) : f(name) {}
};