โž•

C++ Cheat Sheet

C with objects, references, and a huge standard library. The backbone of competitive programming, game engines, and most college data-structures courses. Copy-paste examples plus live Run/Submit exercises.

Jump to: Your first programVariables & typescin / cout ReferencesLoops & ifVectors StringsFunctionsClasses InheritanceTemplatesCommon STL ๐Ÿš€ Practice problems

Your first program

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, world!" << endl;
    return 0;
}
๐Ÿ“ Compile with g++ hello.cpp -o hello, then run ./hello โ€” same compile-then-run model as C.

Variables & types

int age = 25;
double price = 9.99;
char grade = 'A';
bool is_valid = true;   // C++ has a real bool, unlike plain C
auto x = 42;       // auto: let the compiler infer the type

cin / cout

int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old" << endl;

References

A reference is an alias for an existing variable โ€” similar to a pointer, but you use it exactly like the original variable (no * needed).

void increment(int& n) {   // & = pass by reference
    n++;                       // modifies the CALLER's variable directly
}

int main() {
    int x = 5;
    increment(x);
    cout << x << endl;    // 6
}

Loops & if

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

int age = 20;
if (age >= 18) {
    cout << "Adult" << endl;
} else {
    cout << "Minor" << endl;
}

Vectors

A vector is a resizable array โ€” the C++ default over fixed-size C arrays.

#include <vector>

vector<int> nums = {10, 20, 30};
nums.push_back(40);          // add to the end
cout << nums[0] << endl;      // 10
cout << nums.size() << endl;   // 4

for (int n : nums) {        // range-based for loop
    cout << n << endl;
}

Strings

Unlike C, C++'s string is a real type โ€” resizable, with built-in operators.

#include <string>

string name = "Mo";
name += " Salah";               // concatenate with +=
cout << name.length() << endl;   // 8
cout << name.substr(0, 2) << endl; // "Mo"
if (name == "Mo Salah") cout << "Match!" << endl;

Functions

int add(int a, int b) {
    return a + b;
}

// default arguments
void greet(string name = "friend") {
    cout << "Hi, " << name << "!" << endl;
}

Classes

C++'s object system โ€” a class bundles data (fields) and behavior (methods) together.

class Dog {
public:
    string name;
    int age;

    Dog(string n, int a) {   // constructor
        name = n;
        age = a;
    }

    void bark() {
        cout << name << " says woof!" << endl;
    }
};

int main() {
    Dog d("Rex", 3);
    d.bark();               // Rex says woof!
    cout << d.age << endl; // 3
}

Inheritance

class Animal {
public:
    void eat() { cout << "Eating..." << endl; }
};

class Cat : public Animal {   // Cat inherits everything from Animal
public:
    void meow() { cout << "Meow!" << endl; }
};

// Cat c; c.eat(); c.meow();  -- both work

Templates

Write one function/class that works with any type โ€” C++'s version of generics.

template <typename T>
T maxVal(T a, T b) {
    return (a > b) ? a : b;
}

// maxVal(3, 7) -> 7          (works with int)
// maxVal(2.5, 1.1) -> 2.5    (works with double, same function)

Common STL containers

#include <map>
#include <set>

map<string, int> ages;     // key -> value, like a Python dict
ages["Sam"] = 25;
cout << ages["Sam"] << endl;

set<int> seen = {1, 2, 2, 3};  // duplicates auto-removed -> {1,2,3}
cout << seen.size() << endl;  // 3

๐Ÿš€ Practice problems

Full runnable programs, compiled and run in a real C++ sandbox โ€” same workspace as the other cheat sheets.

Sum a vector EASY

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {4, 8, 15, 16, 23, 42};
    int sum = 0;
    for (int n : nums) sum += n;
    cout << sum << endl;
    return 0;
}

Palindrome check MEDIUM

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isPalindrome(string s) {
    string rev = s;
    reverse(rev.begin(), rev.end());
    return s == rev;
}

int main() {
    cout << (isPalindrome("racecar") ? "true" : "false") << endl;
    return 0;
}

Class with a method MEDIUM

#include <iostream>
using namespace std;

class Rectangle {
public:
    double width, height;
    Rectangle(double w, double h) { width = w; height = h; }
    double area() { return width * height; }
};

int main() {
    Rectangle r(4, 5);
    cout << r.area() << endl;
    return 0;
}

Map word counts HARD

#include <iostream>
#include <map>
#include <sstream>
using namespace std;

int main() {
    string text = "the cat sat on the mat the cat ran";
    map<string, int> counts;
    stringstream ss(text);
    string word;
    while (ss >> word) counts[word]++;

    for (auto& pair : counts) {
        cout << pair.first << ": " << pair.second << endl;
    }
    return 0;
}