C++ Cheat Sheet
Free C++ cheat sheet: pointers, STL containers, classes, and memory management at a glance.
Basics
| Concept | Syntax | Example |
|---|---|---|
| Include | #include <iostream> | #include <vector> |
std::cout << value << std::endl | cout << "Hello" << endl; | |
| Input | std::cin >> variable | cin >> name; |
| Namespace | using namespace std; | Avoids writing std:: prefix |
Learn it in a lesson: Introduction to C++ · Input & Output (cin & cout) · Namespaces
Memory
| Concept | Syntax | Example |
|---|---|---|
| Pointer | type* ptr = &var | int* p = &x; |
| Reference | type& ref = var | int& r = x; // alias for x |
| Dynamic alloc | new / delete | int* p = new int(5); delete p; |
| Smart pointer | unique_ptr, shared_ptr | auto p = make_unique<int>(42); |
Learn it in a lesson: Pointers & References · Smart Pointers
Classes & OOP
| Concept | Syntax | Example |
|---|---|---|
| Class | class Name { public: ... }; | class Dog { public: string name; }; |
| Constructor | Name(params) : init { } | Dog(string n) : name(n) {} |
| Inheritance | class B : public A { }; | class Puppy : public Dog {}; |
| Virtual | virtual void fn() = 0; | Pure virtual = abstract method |
Learn it in a lesson: Object-Oriented Programming · Inheritance & Polymorphism
STL Containers
| Concept | Syntax | Example |
|---|---|---|
| Vector | vector<T> v | vector<int> v = {1, 2, 3}; |
| Map | map<K,V> m | map<string,int> ages; |
| Set | set<T> s | set<int> unique = {1, 2, 3}; |
| Unordered map | unordered_map<K,V> | unordered_map<string,int> fast; |
Learn it in a lesson: Maps & Sets · Vectors (std::vector)
Algorithms
| Concept | Syntax | Example |
|---|---|---|
| Sort | sort(begin, end) | sort(v.begin(), v.end()); |
| Find | find(begin, end, val) | auto it = find(v.begin(), v.end(), 5); |
| Transform | transform(in, in_end, out, fn) | transform(v.begin(), v.end(), v.begin(), [](int x){return x*2;}); |
| Accumulate | accumulate(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
| Concept | Syntax | Example |
|---|---|---|
| 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 destructor | std::lock_guard<std::mutex> lock(m); |
Learn it in a lesson: Memory Management · Move Semantics & Smart Pointers · References
Modern C++
| Concept | Syntax | Example |
|---|---|---|
| Type inference Use it where the type is obvious or unspeakable; spell it out where it aids the reader. | auto | auto 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] = pair | for (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. | nullptr | int* 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
| Concept | Syntax | Example |
|---|---|---|
| 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::stoi | std::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_view | void log(std::string_view msg); |
Learn it in a lesson: Strings (std::string) · Input & Output (cin & cout) · string_view & Non-Owning Views