AI Generate C++ docs instantly

C++ Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

5 snippets

Your first steps with C++ basics

Hello World

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Comments

// Single line comment
/* Multi-line
   comment */

User Input

std::string name;
std::cout << "Name: ";
std::cin >> name;

Type Casting

static_cast<int>(3.14)    // C++ style
(int)3.14                  // C style
std::stoi("42")            // string to int

Compile & Run

g++ -o main main.cpp  // Compile
./main                // Run

Variables & Data Types

9 snippets

Store and manage data in C++

Int

int age = 25;

Double

double price = 19.99;

String

std::string name = "John";

Bool

bool isActive = true;

Auto

auto count = 10;

Const

const double PI = 3.14159;

Array

int nums[] = {1, 2, 3, 4, 5};

Vector

std::vector<int> vec = {1, 2, 3};

Map

std::map<std::string, int> ages;

Operators

7 snippets

Symbols for calculations and comparisons

Arithmetic

+ - * /    // Add, Sub, Mul, Div
%          // Modulus
++ --      // Increment/Decrement

Comparison

==  !=     // Equal, Not equal
>   <      // Greater, Less than
>=  <=     // Greater/Less or equal
<=>        // Spaceship (C++20)

Logical

&&   // AND
||   // OR
!    // NOT

Assignment

=   +=  -=  *=  /=  %=
&=  |=  ^=  <<= >>=

Bitwise

&   // AND
|   // OR
^   // XOR
~   // NOT
<<  // Left shift
>>  // Right shift

Pointer/Reference

&x   // Address of x
*p   // Value at pointer p
->   // Member access via pointer

Memory

new    // Allocate
delete // Deallocate
sizeof(int)  // Size in bytes

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Loops

9 snippets

Repeat code efficiently with iterations

For

for (int i = 0; i < 5; i++) {
    std::cout << i;
}

Range-based For

for (const auto& item : vec) {
    std::cout << item;
}

While

while (count < 10) {
    count++;
}

Do While

do {
    count++;
} while (count < 10);

Break

for (int i = 0; i < 10; i++) {
    if (i == 5) break;
}

Continue

for (int i = 0; i < 10; i++) {
    if (i == 5) continue;
    std::cout << i;
}

Iterator Loop

for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it;
}

For Each (Algorithm)

#include <algorithm>
std::for_each(vec.begin(), vec.end(),
    [](int n) { std::cout << n; });

Indexed Range

for (size_t i = 0; i < vec.size(); i++) {
    std::cout << i << ": " << vec[i];
}

Conditionals

5 snippets

Direct program execution with conditions

If/Else

if (x > 0) {
    std::cout << "positive";
} else if (x < 0) {
    std::cout << "negative";
} else {
    std::cout << "zero";
}

Ternary

int max = (a > b) ? a : b;

Switch

switch (day) {
    case 1:
        std::cout << "Monday";
        break;
    default:
        std::cout << "Other";
}

If with Init (C++17)

if (auto it = map.find(key); it != map.end()) {
    std::cout << it->second;
}

Constexpr If (C++17)

if constexpr (std::is_integral_v<T>) {
    // Compile-time branch
}

Functions

5 snippets

Reusable blocks of code and lambdas

Function

std::string greet(std::string name) {
    return "Hello, " + name + "!";
}

Reference

void increment(int& x) {
    x++;
}

Const Ref

void print(const std::string& s) {
    std::cout << s;
}

Default Param

void log(std::string msg, int level = 0) {}

Lambda

auto add = [](int a, int b) { return a + b; };

Pointers & Memory

5 snippets

Pointer

int* ptr = &x;

Dereference

int value = *ptr;

New/Delete

int* p = new int(5);
delete p;

Smart Pointer

std::unique_ptr<int> ptr = std::make_unique<int>(5);

Shared Pointer

std::shared_ptr<int> ptr = std::make_shared<int>(5);

Classes & OOP

3 snippets

Class

class Dog {
private:
    std::string name;
public:
    Dog(std::string n) : name(n) {}
    std::string bark() {
        return name + " says woof!";
    }
};

Inheritance

class Puppy : public Dog {
public:
    Puppy(std::string n) : Dog(n) {}
};

Virtual

virtual void speak() = 0;  // Pure virtual

STL Algorithms

4 snippets

Sort

std::sort(vec.begin(), vec.end());

Find

auto it = std::find(vec.begin(), vec.end(), 5);

Transform

std::transform(vec.begin(), vec.end(), vec.begin(),
    [](int x) { return x * 2; });

Accumulate

int sum = std::accumulate(vec.begin(), vec.end(), 0);

Error Handling

2 snippets

Try/Catch

try {
    throw std::runtime_error("Error");
} catch (const std::exception& e) {
    std::cerr << e.what();
}

Throw

throw std::invalid_argument("Invalid input");

More Cheat Sheets

FAQ

Frequently asked questions

What is a C++ cheat sheet?

A C++ cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in C++. It helps developers quickly look up syntax without searching through documentation.

How do I learn C++ quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important C++ concepts?

Key C++ concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my C++ code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your C++ code using AI.

Code Conversion Tools

Convert C++ to Other Languages

Easily translate your C++ code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate C++ Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week