glunty

C++ Language Cheat Sheet

A searchable reference of common C++ snippets and what each one does.

Common C++ snippets with a plain-English description of what each one does
Snippet What it does
#include <iostream> Bring in the standard input and output stream library.
int main() { return 0; } Program entry point; it returns an int exit code to the system.
std::cout << "Hello"; Print a value to standard output.
std::cin >> age; Read a whitespace-separated value from standard input into age.
int count = 42; Declare an integer variable and set it to 42.
double pi = 3.14; Declare a double-precision floating-point number.
bool ready = true; Declare a boolean that holds true or false.
char letter = 'A'; Store one character; char literals use single quotes.
auto value = 3.5; Let the compiler deduce the variable type from its initializer.
const int MAX = 100; Name a value that cannot be reassigned after initialization.
namespace app { } Group related names under a scope to avoid collisions.
int* ptr = &x; Declare a pointer that stores the memory address of x.
*ptr = 5; Dereference a pointer to read or write the value it points to.
&x The address-of operator yields a pointer to x.
int& ref = x; Declare a reference, an alias for an existing variable.
int* p = nullptr; A null pointer that points at nothing; prefer it over NULL or 0.
int* a = new int(7); Allocate an int on the heap and return a pointer to it.
delete a; Release heap memory obtained from new to avoid a leak.
int* arr = new int[n]; delete[] arr; Allocate then free a heap array using new[] and delete[].
std::unique_ptr<int> u = std::make_unique<int>(5); Sole-ownership smart pointer that frees memory automatically and cannot be copied.
std::shared_ptr<int> s = std::make_shared<int>(5); Reference-counted smart pointer that frees when the last owner goes away.
int local = 5; A stack variable is freed automatically at the end of its scope, unlike heap memory.
std::vector<int> v; A dynamic array that grows as you add elements.
v.push_back(9); Append a value to the end of a vector.
v.size(); Return how many elements the container currently holds.
v.at(2); Access an element with bounds checking that throws if out of range.
std::string s = "hi"; A growable sequence of characters.
std::array<int, 3> a = {1, 2, 3}; A fixed-size array whose length is known at compile time.
std::map<std::string, int> m; Sorted key-value container that keeps keys unique.
std::unordered_map<std::string, int> um; Hash-based key-value container with average constant-time lookup.
std::pair<int, int> p = {1, 2}; Hold two values together, reached as first and second.
auto it = v.begin(); An iterator points into a container; begin() is the first element and end() is one past the last.
for (const auto& x : v) { } Range-based for loop that reads each element of a container by reference.
if (x > 0) { } else { } Run one block when a condition is true and another when it is false.
else if (x == 0) { } Test a further condition when the previous one was false.
for (int i = 0; i < n; ++i) { } Counting loop with an init, a condition, and an update step.
while (cond) { } Repeat a block as long as the condition stays true.
do { } while (cond); Loop that runs the body once before testing the condition.
switch (x) { case 1: break; default: break; } Branch on the value of an integer or enum; break stops fall-through.
int m = (a > b) ? a : b; Ternary operator that picks between two values based on a condition.
for (int x : arr) { } Range-based for visits each element without managing an index.
int add(int a, int b) { return a + b; } Define a function that returns the sum of two ints.
int square(int n); A function declaration, or prototype, with the body defined later.
return result; Hand a value back to the caller and exit the function.
void greet(std::string name = "friend") { } Default argument used when the caller omits that parameter.
void inc(int& n) { ++n; } Pass by reference so the function can change the caller variable.
void print(const std::string& s) { } Pass a large object by const reference to avoid a copy.
auto f = [](int x) { return x * 2; }; A lambda, an inline anonymous function you can store and call.
int area(int); double area(double); Overloading: reuse a name with different parameter types.
template <typename T> T largest(T a, T b) { } A function template that works for any type T.
class Point { }; Define a class, a user type that bundles data and behavior.
struct P { int x; }; A struct is a class whose members default to public.
class C { public: int x; private: int y; }; Public members are reachable anywhere; private members only inside the class.
Point(int x) : x_(x) { } A constructor initializes a new object, here via a member initializer list.
~Point() { } A destructor runs cleanup when the object is destroyed.
class Dog : public Animal { }; Inheritance: Dog gains the members of the base class Animal.
virtual void speak(); A virtual function can be overridden and is dispatched at runtime.
void speak() override; override marks a function that replaces a virtual base function, and the compiler checks it.
bool operator==(const Point& o) const { } Operator overloading defines how == behaves for your type.
this->x = x; this is a pointer to the current object inside a member function.

Runs entirely in your browser. Nothing you type is sent anywhere; open DevTools and watch the Network tab to verify zero requests.

What this tool does

This is a searchable quick reference for the C++ syntax you reach for every day, from printing with std::cout to pointers, containers, control flow, functions, and classes. Each row pairs a short snippet with a plain-English note on what it does. Type in the filter box to search across both columns, or tap a category button to focus on one area. The whole list is built into the page, so it works offline and sends nothing anywhere.

How to use it

Start typing in the Filter box. Entering vector jumps to the container snippets; entering pointer or nullptr surfaces the memory rows; entering lambda shows the inline function form. The category buttons (Basics, Memory, Containers, Control flow, Functions, Classes) narrow the table to one area and combine with the text filter, so you can pick Classes and type virtual to zero in on runtime dispatch. Clear the box to see the full sheet again.

Common use cases

  • Recalling the exact form of a snippet you use rarely, such as make_unique or an operator overload.
  • Copying a correct line into your editor instead of guessing the syntax.
  • Teaching a beginner the difference between a pointer, a reference, and a value.
  • Checking modern habits, like reaching for auto, nullptr, and smart pointers over older style.
  • Reviewing core C++ before an interview or a new project.

Common pitfalls

  • Forgetting to free memory. Every new needs a matching delete, and every new[] a matching delete[]. Prefer smart pointers so cleanup happens automatically.
  • Dangling and null pointers. Using a pointer after its memory is freed, or dereferencing nullptr, is undefined behavior. Set freed pointers to nullptr and check before dereferencing.
  • Bounds errors. Indexing a vector past its size with the subscript operator does not check bounds; use at when you want a thrown error instead of silent corruption.
  • Object slicing. Assigning a derived object into a base value copies only the base part and drops the rest. Hold polymorphic objects through a reference or pointer, and mark overridable functions virtual.

Frequently asked questions

What is a smart pointer in C++?
A smart pointer is an object that owns a heap allocation and frees it automatically when it goes out of scope, which prevents memory leaks. std::unique_ptr models sole ownership and cannot be copied, while std::shared_ptr keeps a reference count and releases the memory once the last owner is gone. Reaching for smart pointers instead of raw new and delete is the modern default in C++11 and later.
What is the difference between a pointer and a reference?
A pointer is a variable that stores a memory address; it can be null, can be reassigned to point elsewhere, and you dereference it with the star operator. A reference is an alias for an existing object; it must be bound when created, cannot be rebound, and is used with the same syntax as the original variable. Pointers are for optional or reseatable links, while references are for a guaranteed, fixed alias.
What is a template in C++?
A template is a pattern that lets you write a function or class once and have the compiler generate a version for each type you use. The syntax template with a typename parameter defines a placeholder type that is filled in when you call the function with ints, doubles, or your own types. Standard containers like std::vector and std::map are templates, which is why they can hold any element type.
When should I use std::vector versus std::array?
Use std::vector when the number of elements can change at runtime; it manages a resizable buffer on the heap and grows as you push elements. Use std::array when the size is fixed and known at compile time; it stores its elements inline with no heap allocation and no growth. In short, std::vector trades a little overhead for flexibility, while std::array is leaner but fixed in length.
What is RAII in C++?
RAII stands for Resource Acquisition Is Initialization, a pattern where an object acquires a resource in its constructor and releases it in its destructor. Because the destructor runs automatically when the object leaves scope, files, locks, and memory get cleaned up even if an error occurs. Smart pointers and standard containers rely on RAII, which is why manual cleanup is rarely needed in modern C++.
Does this C++ cheat sheet send my searches anywhere?
No. The entire snippet list is baked into the page and every search and filter runs in your browser with JavaScript, so nothing you type leaves your device. Open your browser DevTools and watch the Network tab while you search to confirm there are zero requests.

Embed this tool

Free for any use; attribution appreciated. Paste this on your site:

The embed runs the same tool that lives at this URL. No tracking; no ads inside the embed. Resize height as needed for your layout.

Cite this tool

For academic, journalistic, or technical references. Pick a format:

Citations use 2026 as the publication year. Access date is left as a fillable placeholder where the citation style expects one.

Embedded tool from glunty.com