C++ Cheatsheet - C++ Syntax & STL Reference
This reference is for C++ developers writing applications, games, or libraries, organized around modern C++ — where smart pointers, the STL, and lambdas drastically reduce the manual-lifetime bugs C++ is famous for. It covers classes and objects with their access control, the go-to STL containers and when each fits, smart pointers vs raw new/delete, lambda captures, function and class templates, and exceptions. Unlike a flat C++ feature list, entries are grouped by the construct you are writing and flag RAII-versus-manual-ownership choices. After reading you should be able to reach for std::vector/std::map instead of raw arrays, prefer unique_ptr over naked new, and write a templated function or lambda capture-free of leaks.
Classes & Objects 8
class MyClass { public: MyClass(); ~MyClass(); };struct MyStruct { int x; int y; };MyClass obj;MyClass *obj = new MyClass();delete obj;class Derived : public Base { };virtual void func() override;class Abstract { virtual void func() = 0; };STL Containers 8
std::vector<int> v = {1, 2, 3};v.push_back(4); v.size(); v[0];std::list<int> l;std::map<std::string, int> m;std::unordered_map<std::string, int> m;std::set<int> s;std::stack<int> st; std::queue<int> q;std::pair<int, std::string> p = {1, "one"};Smart Pointers 5
std::unique_ptr<int> p = std::make_unique<int>(10);std::shared_ptr<int> p = std::make_shared<int>(10);std::weak_ptr<int> wp = sp;if (auto locked = wp.lock()) { }std::unique_ptr<int> p2 = std::move(p);Lambda Expressions 6
auto fn = []() { return 42; };int x = 10; auto fn = [x]() { return x; };auto fn = [&x]() { x++; };auto fn = [=]() { return x; };auto fn = [&]() { x++; };auto fn = [](int a, int b) { return a + b; };Templates 4
template <typename T> T add(T a, T b) { return a + b; }template <typename T> class Stack { };template <> void func<int>(int x) { }std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });Exception Handling 5
try { } catch (const std::exception &e) { }throw std::runtime_error("error");catch (...) { }noexceptstd::terminate();Modern C++ Features 8
auto x = 42;decltype(x) y = 10;constexpr int MAX = 100;std::optional<int> opt = 42;std::variant<int, std::string> v = 42;std::any a = 42;for (auto &[key, value] : map) { }std::string_view sv = "hello";Tips
- Prefer smart pointers over manual new/delete.
- STL container choice: vector for random access, list for frequent inserts/deletes, unordered_map for key lookups.
- Lambdas suit short callbacks; use a plain function or functor for complex logic.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us