Quick Reference
Modern C++ Cheat Sheet
Syntax, pointers, OOP, STL containers, algorithms, and complexity at a glance.
1. Program Structure & Fast I/O
#include <iostream>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << "Hello C++\n";
return 0;
}2. Smart Pointers & RAII (<memory>)
auto u = std::make_unique<int>(42); // Exclusive ownership
auto s = std::make_shared<int>(100); // Reference-counted shared
std::weak_ptr<int> w = s; // Non-owning observer3. OOP & Virtual Polymorphism
class Base {
public:
virtual void run() const = 0;
virtual ~Base() = default;
};
class Derived : public Base {
public:
void run() const override {}
};4. STL Containers & Time Complexities
std::vector<int> v; // O(1) push_back, O(1) index
std::unordered_map<int, int> mp; // O(1) avg lookup/insert
std::map<int, int> omp; // O(log n) sorted keys
std::priority_queue<int> pq; // O(log n) push/pop, O(1) top5. STL Algorithms (<algorithm> & <numeric>)
std::sort(v.begin(), v.end());
bool ok = std::binary_search(v.begin(), v.end(), 10);
auto it = std::lower_bound(v.begin(), v.end(), 10);
int sum = std::accumulate(v.begin(), v.end(), 0);6. Modern C++17 / C++20 Essentials
auto [k, val] = std::pair{1, "CodeZov"}; // Structured binding (C++17)
std::optional<int> opt = 42; // Optional value (C++17)
auto fn = [](auto a, auto b) { return a + b; }; // Generic lambda