C++ Cheat Sheet

Free C++ cheat sheet: pointers, STL containers, classes, and memory management at a glance.

Basics

ConceptSyntaxExample
Include#include <iostream>#include <vector>
Printstd::cout << value << std::endlcout << "Hello" << endl;
Inputstd::cin >> variablecin >> name;
Namespaceusing namespace std;Avoids writing std:: prefix

Learn it in a lesson: Introduction to C++ · Input & Output (cin & cout) · Namespaces

Memory

ConceptSyntaxExample
Pointertype* ptr = &varint* p = &x;
Referencetype& ref = varint& r = x; // alias for x
Dynamic allocnew / deleteint* p = new int(5); delete p;
Smart pointerunique_ptr, shared_ptrauto p = make_unique<int>(42);

Learn it in a lesson: Pointers & References · Smart Pointers

Classes & OOP

ConceptSyntaxExample
Classclass Name { public: ... };class Dog { public: string name; };
ConstructorName(params) : init { }Dog(string n) : name(n) {}
Inheritanceclass B : public A { };class Puppy : public Dog {};
Virtualvirtual void fn() = 0;Pure virtual = abstract method

Learn it in a lesson: Object-Oriented Programming · Inheritance & Polymorphism

STL Containers

ConceptSyntaxExample
Vectorvector<T> vvector<int> v = {1, 2, 3};
Mapmap<K,V> mmap<string,int> ages;
Setset<T> sset<int> unique = {1, 2, 3};
Unordered mapunordered_map<K,V>unordered_map<string,int> fast;

Learn it in a lesson: Maps & Sets · Vectors (std::vector)

Algorithms

ConceptSyntaxExample
Sortsort(begin, end)sort(v.begin(), v.end());
Findfind(begin, end, val)auto it = find(v.begin(), v.end(), 5);
Transformtransform(in, in_end, out, fn)transform(v.begin(), v.end(), v.begin(), [](int x){return x*2;});
Accumulateaccumulate(begin, end, init)int sum = accumulate(v.begin(), v.end(), 0);

Learn it in a lesson: STL Algorithm Mastery: Transformations, Predicates & Custom Conditions

Memory & Ownership

ConceptSyntaxExample
Unique ownership
Frees itself when it goes out of scope. Cannot be copied, only moved.
std::unique_ptr<T>auto p = std::make_unique<Node>(4);
Shared ownership
Reference-counted. Two shared_ptrs pointing at each other never free — break the cycle with weak_ptr.
std::shared_ptr<T>auto p = std::make_shared<Node>(4);
Move instead of copy
Hands over the guts rather than duplicating them. The source is left valid but unspecified.
std::move(x)v.push_back(std::move(big));
References
Pass big objects by const reference to avoid a copy.
T& | const T&void print(const std::string& s);
RAII
The single most important C++ idea: leaving the scope cleans up, even when an exception is thrown.
acquire in the constructor, release in the destructorstd::lock_guard<std::mutex> lock(m);

Learn it in a lesson: Memory Management · Move Semantics & Smart Pointers · References

Modern C++

ConceptSyntaxExample
Type inference
Use it where the type is obvious or unspeakable; spell it out where it aids the reader.
autoauto it = v.begin();
Range-for
Take by const reference or you copy every element.
for (const auto& x : v)for (const auto& name : names) std::cout << name;
Lambda[capture](args) { }auto add = [](int a, int b) { return a + b; };
Structured bindings
C++17. Much clearer than .first and .second.
auto [a, b] = pairfor (const auto& [key, value] : m) { }
Optional
Says "maybe no value" in the type, instead of a magic -1.
std::optional<T>std::optional<int> find();
Nullptr
Never NULL or 0 in modern C++ — nullptr has a real type.
nullptrint* p = nullptr;

Learn it in a lesson: C++17 & C++20 Modern Features (Structured Bindings, Concepts, Ranges) · auto & decltype · Range-Based for Loops · Lambda Expressions

Strings & I/O

ConceptSyntaxExample
Read a line
Use this rather than >> when the input contains spaces.
std::getline(std::cin, s)std::string line; std::getline(std::cin, line);
Find
Returns npos when not found, not -1.
s.find(x)if (s.find("cat") != std::string::npos) { }
Number to string
stoi throws on junk input — wrap it if the string came from a user.
std::to_string | std::stoistd::stoi("42") // 42
Cheap read-only view
C++17. No copy — but it does not own the characters, so never outlive the source.
std::string_viewvoid log(std::string_view msg);

Learn it in a lesson: Strings (std::string) · Input & Output (cin & cout) · string_view & Non-Owning Views