C++ Cheatsheet - C++ Syntax & STL Reference
All essential C++ commands organized by use case, with 44+ entries you can copy and run directly. Find the right command fast when you need it.
Back to LanguagesClasses & Objects 8
class MyClass { public: MyClass(); ~MyClass(); };Class definition
struct MyStruct { int x; int y; };Struct (public by default)
MyClass obj;Stack object
MyClass *obj = new MyClass();Heap object
delete obj;Delete an object
class Derived : public Base { };Inheritance
virtual void func() override;Override a virtual function
class Abstract { virtual void func() = 0; };Pure virtual function (abstract class)
STL Containers 8
std::vector<int> v = {1, 2, 3};Dynamic array
v.push_back(4); v.size(); v[0];vector operations
std::list<int> l;Doubly linked list
std::map<std::string, int> m;Ordered map (red-black tree)
std::unordered_map<std::string, int> m;Unordered map (hash table)
std::set<int> s;Ordered set
std::stack<int> st; std::queue<int> q;Stack and queue
std::pair<int, std::string> p = {1, "one"};Key-value pair
Smart Pointers 5
std::unique_ptr<int> p = std::make_unique<int>(10);Exclusive ownership pointer
std::shared_ptr<int> p = std::make_shared<int>(10);Shared pointer (ref-counted)
std::weak_ptr<int> wp = sp;Weak reference (no ref count)
if (auto locked = wp.lock()) { }Weak to strong reference
std::unique_ptr<int> p2 = std::move(p);Transfer ownership
Lambda Expressions 6
auto fn = []() { return 42; };Non-capturing lambda
int x = 10; auto fn = [x]() { return x; };Capture by value
auto fn = [&x]() { x++; };Capture by reference
auto fn = [=]() { return x; };Capture all by value
auto fn = [&]() { x++; };Capture all by reference
auto fn = [](int a, int b) { return a + b; };Lambda with parameters
Templates 4
template <typename T> T add(T a, T b) { return a + b; }Function template
template <typename T> class Stack { };Class template
template <> void func<int>(int x) { }Template specialization
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });Custom sort
Exception Handling 5
try { } catch (const std::exception &e) { }Catch an exception
throw std::runtime_error("error");Throw an exception
catch (...) { }Catch all exceptions
noexceptDeclare no-throw
std::terminate();Terminate the program
Modern C++ Features 8
auto x = 42;Type deduction
decltype(x) y = 10;Declared type
constexpr int MAX = 100;Compile-time constant
std::optional<int> opt = 42;Optional value
std::variant<int, std::string> v = 42;Variant type
std::any a = 42;Any type
for (auto &[key, value] : map) { }Structured binding (C++17)
std::string_view sv = "hello";String view (C++17)
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
Commands are compiled from the official docs below. Click to verify the latest usage.
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