| Learning Resources CheatsheetA complete C++ cheatsheet — every major concept in one page: basics, control flow, OOP, modern features, and the STL. Ideal for last-minute revision and day-to-day lookup. Quick Reference C++ Programming CheatsheetEvery major C++ concept on one page — basics, OOP, modern features, and the STL — with tables and the smallest working snippets. Built for revision before interviews and for looking things up while you code. Program Structure & I/OMinimal program Compile and run cin / cout essentials Tip: Prefer Data Types, Variables & ConstantsCommon types (typical 64-bit)
const / constexpr / auto / enum class Operators
Cannot overload: Control Flow Arrays C-arrays decay to pointers and have no bounds checks. Prefer Strings ( |
| Feature | Pointer | Reference |
|---|---|---|
| Null? | Yes (nullptr) | No (must bind) |
| Reseat? | Yes | No |
| Syntax | *p / &x | T& r = x |
| Arithmetic | Yes | No |
int x = 10;
int* p = &x; *p = 20;
int& r = x; r = 30;
int* q = nullptr;
Namespaces, Structures & Unions
namespace util { int clamp(int x); }
util::clamp(5);
namespace u = util;
struct Point { int x, y; }; // public by default
union U { int i; float f; };
File Handling & Command Line
#include <fstream>
ofstream out("out.txt");
out << "line\n";
ifstream in("out.txt");
string line;
while (getline(in, line)) cout << line << '\n';
int main(int argc, char* argv[]) { ... }
OOP — Classes & Objects
class Account {
public:
explicit Account(int bal) : balance(bal) {}
~Account() = default;
int get() const { return balance; }
static int count;
private:
int balance;
friend void audit(Account&);
};
| Idea | Cheat note |
|---|---|
| struct vs class | Default access public vs private |
| Encapsulation | private data + public API |
this | Pointer to current object |
| Static members | Shared by all instances |
| Friend | Access private; use sparingly |
| Constructors / Destructors | Init and RAII cleanup |
| Rule of Zero/Five | RAII types or define/delete specials |
= default / = delete | Generate or forbid specials |
Inheritance, Abstraction & Polymorphism
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
};
class Circle : public Shape {
public:
double area() const override;
};
Shape* p = new Circle(...);
p->area();
delete p; // needs virtual ~Shape
Keywords: virtual, override, final, access levels. Diamond problem → virtual inheritance. Overload = compile-time; override = runtime.
Modern C++ — RAII, Smart Ptrs, Move, Lambda
Smart pointers & RAII
#include <memory>
auto u = make_unique<T>(...); // exclusive
auto s = make_shared<T>(...); // shared
weak_ptr<T> w = s; // non-owning / break cycles
Move & lambda
T b = std::move(a);
auto f = [=](int x) { return x + 1; };
auto g = [&out]() { out++; };
| Cast | Use |
|---|---|
static_cast | Related conversions |
dynamic_cast | Safe polymorphic downcast |
const_cast | Add/remove const |
reinterpret_cast | Low-level (rare) |
Templates & Exceptions
template<typename T>
T maxv(T a, T b) { return a > b ? a : b; }
try {
throw runtime_error("fail");
} catch (const exception& e) {
cerr << e.what();
}
Catch by const&. Member function templates cannot be virtual.
STL Containers
| Container | Header | Notes |
|---|---|---|
vector | <vector> | Dynamic array — default choice |
deque | <deque> | Fast insert/erase at both ends |
list / forward_list | <list> | Doubly / singly linked |
set / multiset | <set> | Sorted unique / duplicates |
unordered_set | <unordered_set> | Hash unique membership |
map / multimap | <map> | Sorted key→value |
unordered_map | <unordered_map> | Hash map / counters |
pair / tuple | <utility> / <tuple> | Fixed-size groups |
vector<int> v = {1, 2, 3};
v.push_back(4);
map<string, int> ages;
ages["Ann"] = 20; // may insert
unordered_map<string, int> freq;
freq["hi"]++;
Algorithms, Iterators, Heaps & Adapters
#include <algorithm>
sort(v.begin(), v.end());
stable_sort(v.begin(), v.end());
find(v.begin(), v.end(), x);
lower_bound(v.begin(), v.end(), x); // needs sorted
min(a, b); max_element(v.begin(), v.end());
make_heap(v.begin(), v.end());
push_heap / pop_heap / sort_heap / is_heap
| Adapter | Order | Header |
|---|---|---|
stack | LIFO | <stack> |
queue | FIFO | <queue> |
priority_queue | Max-heap (default) | <queue> |
stack<int> st; st.push(1); st.top(); st.pop();
priority_queue<int, vector<int>, greater<int>> minh;
Gotchas & Interview Traps
| Trap | Remember |
|---|---|
map[k] missing key | Inserts default — use find/at |
| Non-virtual base destructor | Deleting via base* is UB |
| Object slicing | Pass polymorphic types by pointer/ref |
delete vs delete[] | Must match new / new[] |
| Moved-from object | Valid but unspecified |
string_view lifetime | Must not outlive source |
| STL heap ≠ heap memory | Different meanings |
multiset.erase(key) | Removes all equals |
Lambda [&] | Watch dangling references |
| Integer division | 5/2 == 2 |
How to use: skim jump chips → drill tables → type every snippet once. Pair with Interview Q&A for spoken answers.