C++ cheat sheet
96 one-line examples for C++. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.
Basics and Output
| Code | What it does |
|---|---|
#include <iostream> | Adds input and output tools to the program. |
int main() { } | This is the start point of every program. |
std::cout << "Hi"; | Prints text to the screen. |
std::cout << x; | Prints the value of a variable. |
std::cout << "\n"; | Moves the output to a new line. |
std::endl | Ends the line and flushes the output. |
// comment | The compiler ignores this text. |
/* block */ | The compiler ignores this longer comment. |
return 0; | Ends main and reports success. |
using namespace std; | Lets you skip std:: in small programs. |
#include <string> | Adds the string type to the program. |
std::cin >> x; | Reads one value from the keyboard. |
Variables and Types
| Code | What it does |
|---|---|
int x = 5; | Stores a whole number in x. |
double d = 2.5; | Stores a decimal number in d. |
char c = 'A'; | Stores one character in c. |
bool ok = true; | Stores true or false in ok. |
std::string s = "hi"; | Stores text in s. |
auto n = 10; | Lets the compiler pick the type. |
const int N = 3; | Makes N a value that cannot change. |
int a, b = 0; | Declares two integers in one line. |
x = 7; | Gives x a new value. |
int y = x + 1; | Copies x plus one into y. |
(double)x | Converts x to a double value. |
sizeof(int) | Gives the size of int in bytes. |
Operators
| Code | What it does |
|---|---|
a + b | Adds a and b. |
a - b | Subtracts b from a. |
a * b | Multiplies a and b. |
a / b | Divides a by b. |
a % b | Gives the remainder of a divided by b. |
a++ | Adds one to a after use. |
++a | Adds one to a before use. |
a += 3 | Adds three to a. |
a == b | Tests if a equals b. |
a != b | Tests if a is not equal to b. |
a && b | True only when both are true. |
a || b | True when at least one is true. |
Control Flow
| Code | What it does |
|---|---|
if (x > 0) { } | Runs the block when x is positive. |
else { } | Runs this block when the if test fails. |
else if (x == 0) { } | Tests another case after a failed if. |
for (int i = 0; i < n; i++) | Repeats a block n times. |
while (x > 0) { } | Repeats while the test stays true. |
do { } while (x > 0); | Runs the block once, then tests. |
break; | Leaves the loop at once. |
continue; | Skips to the next loop step. |
switch (x) { case 1: } | Chooses a branch by value. |
case 1: break; | Ends one switch branch. |
default: break; | Handles values with no case match. |
x > 0 ? a : b | Picks a or b by the test. |
Functions
| Code | What it does |
|---|---|
int add(int a, int b) { } | Defines a function that returns an int. |
return a + b; | Sends a value back to the caller. |
void show() { } | Defines a function with no return value. |
add(2, 3); | Calls add with two values. |
int add(int, int); | Declares a function before its body. |
int add(int a = 0) | Gives a parameter a default value. |
void f(int &x) | Passes x by reference so changes stay. |
void f(const int &x) | Passes x by reference without copying. |
int f() { return 1; } | A short function can sit on one line. |
inline int f() | Suggests the compiler copy the code. |
int f(int); | Overloads f with a different parameter list. |
main() calls f() | A function must be declared before use. |
Arrays and Strings
| Code | What it does |
|---|---|
int a[5]; | Makes an array of five integers. |
int a[3] = {1, 2, 3}; | Makes an array with three values. |
a[0] | Reads the first element of a. |
a[2] = 9; | Sets the third element to nine. |
a.size() | Gives the number of elements in a vector. |
std::vector<int> v; | Makes an empty vector of integers. |
v.push_back(5); | Adds five to the end of v. |
s.length() | Gives the number of characters in s. |
s[0] | Reads the first character of s. |
s + " there" | Joins two strings into a new one. |
s.substr(0, 2) | Gives two characters from the start. |
std::getline(std::cin, s); | Reads a full line into s. |
Classes and Objects
| Code | What it does |
|---|---|
class Point { }; | Defines a new class named Point. |
public: | Makes the following members open to all. |
private: | Hides the following members from outside. |
int x; | A member variable inside a class. |
Point() { } | A constructor runs when an object is made. |
Point p; | Makes an object of class Point. |
p.x = 3; | Sets a public member of p. |
int get() const { } | A const method cannot change the object. |
struct P { int x; }; | A struct is a class with public members. |
class B : public A { }; | Class B inherits from class A. |
virtual void f(); | Lets a derived class replace this method. |
~Point() { } | A destructor runs when the object ends. |
Pointers and References
| Code | What it does |
|---|---|
int *p = &x; | p holds the address of x. |
*p = 5; | Sets the value that p points to. |
int &r = x; | r is another name for x. |
int *p = nullptr; | Makes a pointer that points to nothing. |
if (p != nullptr) | Tests that the pointer is safe to use. |
new int(5) | Allocates one integer on the heap. |
delete p; | Frees memory made with new. |
int a[3]; int *p = a; | An array name acts like a pointer. |
p + 1 | Points to the next element. |
std::unique_ptr<int> u; | A smart pointer that frees itself. |
std::make_unique<int>(5) | Makes a unique pointer with value five. |
u.get() | Gives the raw pointer inside u. |