C++
Basics
#include <iostream>—I/O library
#include <string>—String library
#include <vector>—Dynamic array
using namespace std;—Standard namespace
int main() { return 0; }—Entry point
std::cout << "text"—Print to console
std::cin >> var—Read from console
std::endl—Newline + flush
Data Types
int—32-bit integer
long long—64-bit integer
double—64-bit float
float—32-bit float
char—Single character
bool—true/false
std::string—String type
auto—Type inference
const—Constant value
unsigned—Non-negative only
size_t—Unsigned size type
nullptr—Null pointer
STL Containers
vector<T> v—Dynamic array
v.push_back(x)—Add to end
v.size()—Number of elements
v[i] / v.at(i)—Access element
v.begin() / v.end()—Iterators
map<K,V> m—Ordered key-value map
unordered_map<K,V>—Hash map (faster)
set<T> / unordered_set<T>—Unique elements
stack<T> / queue<T>—LIFO / FIFO
deque<T>—Double-ended queue
pair<A,B> p—Two-value tuple
array<T, N>—Fixed-size array
Strings
s.length() / s.size()—String length
s.substr(pos, len)—Substring
s.find("sub")—Find substring position
s.append(str)—Concatenate
s + " text"—String concatenation
s.compare(other)—Compare strings
std::to_string(n)—Number to string
std::stoi(s)—String to int
s.c_str()—C-style string pointer
Control Flow
if (cond) { } else { }—If-else statement
switch (val) { case: }—Switch statement
for (int i=0; i<n; i++)—For loop
for (auto& x : container)—Range-based for
while (cond) { }—While loop
break / continue—Exit / skip loop
cond ? a : b—Ternary operator
Functions
int fn(int a, int b)—Function with return
void fn()—No return value
int fn(int a = 5)—Default parameter
void fn(int& ref)—Pass by reference
void fn(const int& x)—Const reference
auto fn() -> int—Trailing return type
template<typename T>—Function template
[](int x) { return x*2; }—Lambda expression
Pointers & Memory
int* ptr = &var—Pointer to variable
*ptr—Dereference pointer
int& ref = var—Reference to variable
new int(5)—Heap allocation
delete ptr—Free heap memory
std::unique_ptr<T>—Unique ownership
std::shared_ptr<T>—Shared ownership
std::make_unique<T>()—Create unique_ptr
std::make_shared<T>()—Create shared_ptr
Classes
class MyClass { };—Class definition
public: / private:—Access specifiers
MyClass() { }—Constructor
~MyClass() { }—Destructor
class B : public A—Inheritance
virtual void fn()—Virtual method
override—Explicit override
static int count—Static member
STL Algorithms
sort(v.begin(), v.end())—Sort container
reverse(v.begin(), v.end())—Reverse container
find(v.begin(), v.end(), x)—Find element
count(v.begin(), v.end(), x)—Count occurrences
accumulate(v.begin(), v.end(), 0)—Sum elements
min_element / max_element—Min/max iterator
binary_search(b, e, val)—Binary search
transform(b, e, out, fn)—Apply function
allprintabledoc.com