C++ Cheat Sheet
Modern C++ syntax covering pointers, references, STL containers, templates, smart pointers, and RAII memory management.
2 PagesIntermediateApr 12, 2026
Basic Syntax
Variables, control flow, and I/O.
cpp
#include <iostream>using namespace std;int main() { int age = 30; // stack-allocated int double pi = 3.14159; string name = "Ada"; // std::string if (age >= 18) { cout << name << " is an adult" << endl; } for (int i = 0; i < 5; i++) { cout << "Count: " << i << endl; } return 0;}
STL Containers
Vectors, maps, and algorithms.
cpp
#include <vector>#include <map>#include <algorithm>vector<int> nums = {5, 3, 1, 4, 2};sort(nums.begin(), nums.end()); // {1, 2, 3, 4, 5}nums.push_back(6);nums.size(); // 6map<string, int> ages;ages["Alice"] = 30;ages.count("Alice"); // 1for (auto& [name, age] : ages) { // structured bindings (C++17) cout << name << ": " << age << endl;}
Smart Pointers & RAII
Automatic memory management.
cpp
#include <memory>unique_ptr<int> up = make_unique<int>(42); // sole owner, auto-deletedshared_ptr<int> sp = make_shared<int>(10); // reference-countedshared_ptr<int> sp2 = sp; // ref count = 2// avoid raw `new`/`delete` in modern C++class Widget {public: Widget() { cout << "created" << endl; } ~Widget() { cout << "destroyed" << endl; } // RAII cleanup};
Core Keywords
Common modern C++ keywords.
- const- declares a value or method that cannot modify state
- constexpr- evaluated at compile time when possible
- virtual- enables runtime polymorphism via vtable dispatch
- override- marks a method as overriding a virtual base method
- namespace- groups identifiers to avoid naming collisions
- auto- lets the compiler deduce the variable's type
- template<typename T>- defines generic functions/classes parameterized by type
- nullptr- type-safe null pointer constant (replaces NULL/0)
Pro Tip
Follow the Rule of Five: if you define a destructor, copy constructor, copy assignment, move constructor, or move assignment, you likely need to define all five.
Was this cheat sheet helpful?
Explore Topics
#CCheatSheet#Programming#Intermediate#BasicSyntax#STLContainers#SmartPointersRAII#CoreKeywords#Docker#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance