C++


Types

int

Integer

64

short

64

long

64

long long

64

float

Floating point decimal

64.0

double

Double precision floating point decimal

64.0

char

Character

"a"

bool

Boolean

true

Arithmetic operators

+

Addition

1 + 2

Unary positive, makes number positive

+2

-

Subtraction

2 - 1

Unary negative, makes number negative

*

Multiplication

2 * 3

/

Division

4 / 2

%

Modulo (remainder of division)

4 % 2

++

Increment

x++;

--

Decrement

x--;

Assignment operators

=

Equals

x = 2

+=

a+=b is a=a+b

x += 2

-=

a-=b is a=a-b

x -= 2

*=

a*=b is a=a*b

x *= 2

/=

a/=b is a=a/b

x /= 2

%=

a%=b is a=a%b

x %= 2

Comparison operators

==

Is equal to

1 == 2

!=

Is not equal to

1 != 2

>

Greater than

1 > 2

<

Less than

1 < 2

>=

Greater than or equal to

1 >= 2

<=

Less than or equal to

1 <= 2

Bitwise operators

&

Bitwise AND

0b11001010 & 0b00100011

|

Bitwise OR

0b11001010 | 0b00100011

^

Bitwise XOR

0b11001010 ^ 0b00100011

<<

Arithmetic shift left (shifts all the bits one space to the left, essentially multiplying it by 2)

0b11001010 << 2

>>

Logical shift right (shifts all the bits one space to the right, essentially dividing it by 2)

0b11001010 >> 2

~

Bitwise NOT

~0b00100011

Logical operators

&&

AND operator

1 && 0

||

OR operator

1 || 0

!

NOT operator

!1

Miscellaneous operators

<:<

Insertion operator,

,

Links related expressions together

sizeof()

Unary operator that returns how many variables in RAM a variable holds

.

Denotes relation of an object to a struct

math.1

[]

Denotes an object is an array during declaration, also holds the array's size, unless being initialised

int numbers[4];

References a array's index

numbers[3]

()

Denotes parameters of a function

f("object");

Denotes a function

f();

Denotes a parameters when initialising a new instance of a class

Class objectName("parameters");

{}

Defines a block

void foo() {
}

Initialises an array

int numbers[] = {0, 1, 2, 3};

Initialises a variable through brace initialisation, which assigns default values on empty brackets and prohibits illegal conversons

int n {64};

:

Used for switch cases

case 1:

Used for separating true and false returns in conditional expressions

retval = exp ? trueret : falseret

Used for labelling different access levels of a class

public:

?

Used for separating condition and return values in conditional expressions

retval = exp ? trueret : falseret

->

Denotes relation of a pointer to a struct instance variable

px->a;

(*px).a;

*

Unary delcaration of a pointer variable or function; that reads the value in the assigned memory address

int *px = &x;

int *pf)(int) = &f;

Unary dereferencing (associating a pointer to an address)

*p

Pointer typecasting; enforcing a pointer type

(int *) malloc(6 * (int *));

&

Unary memory address of a variable

&x

Referencer; provides an alias for a variable

int& x

<>

Denotes imported module file

#include <iostream>

Denotes wrapper type for calling functions with a template

""

Declares a string

"Hello world!"

''

Declares a string

'Hello world!'

;

Declares end of a line of code

break;

...

Declares multiple arguments

double average(int number, ...) {

sizeof

Finds the size of a datatype

(int *) malloc(sizeof(int));

Finds the size of a variable in bits

int y = sizeof(5+2);

Key word

using

Declares an alias for a type name

using number = int;

using namespace

Declares a namespace; a default class to resolve methods and objects from if a class prefix is not specified

using namespace std;

template

Declares a template; a generic datatype for a class or function

template <typename T> void functionName(T object) {

auto

Declares automatic type; determines type based on context

new

Used for declaring new objects from a class in "Java style"

Class* obj = new Class(parameters);

delete

Used for freeing objects created by "Java style"

class

Declares a class

class Name {
code;
};

private

Used for declaring private classes or methods, that cannot be interacted with by other classes

public

Used for declaring public classes or methods, that can be interacted with by other classes

nullptr

Object representing zero address

double

Declares doubles (also known as floats, or numbers with decimal place)

struct

Declares a structure; which defines public custom datatypes similar to a class, but without methods

struct Foo {
 code
};

typedef struct {
 code;
} Foo;

int

Declares integers

goto

Goes to a label

if

Executes code if condition is true

if (condition) {
 code;
}

void

Used for declaring that a method does not return a specific value

extern

Used for declaring a variable, but not defining it

static

Declares a static variable; a variable that retains its value outside of its scope

while

Loops code while a condition is true

while (condition) {
 code;
}

do {
 code;
}
while (condition);

for

Loops code for a set amount iterations or for every object in list

for (initialization; condition; update) {
 code;
}

register

Used for storing value in a CPU register (I think this is deprecated, but wow)

do

for declaring loop in a while loop

else

Executes code when if or else if isn't executed

import

Imports classes and modules

return

Terminates a function, returning a value

continue

Used to tell compiler to complete the next iteration of a loop

switch

Switches logic flow based on an output

switch (expression) {
case x:
  code;
break;
case y:
  code;
break;
}

break

Breaks from

typedef

Used for creating name or an alias for a type (particularly used for aliases and for nominating structs)

union

Declares a union; a structure containing one object that can interpret a datarange as the types specified in the union

union Foo {
 code;
};

else if

Executes code on true condition if the last if statement was false

Constructor

Constructor of a class (method to initialise instaces of a class)

class() {
 code
}

Deconstructor

Deconstructor of a class (method to destroy instance of class)

~class() {
 code
}

<algorithm>

<vector>

Dynamic array

<list>

Linked list

<unordered_set>

Hash table with key values

<unordered_set>

Hash table with key value and data value pairs

<iostream>

Input and output control

<array>

<deque>

<stack>

<queue>

<iterator>

cout

Character out; object representing output stream

std::cout << "Hello world";

endl

End line; flushes the iostream buffer to the terminal and indents a new line

cin

Character in; object representing input stream

std::cin >> "Hello world";

size_t

Special type for sequence container sizes

std::cin >> "Hello world";

<stdlib.h>

malloc

Memory Allocation; Allocates block of RAM in heap for an object

(type *) malloc(size);

calloc

Continuous Allocation; Allocates blocks of RAM for a variable contiguously, setting the variables space to all 0s

(type *) calloc(nitems, size);

free

Deallocates RAM for a variable

free(*p);

Argument specifiers

%s

String

%d

Integer

%f

Float

%.1f

Float with 1 decimal place, the 1 can be modified to imply more decimal places

%x

Integer in hexadecimal (lowercase a b c d e f)

%X

Integer in hexadecimal (uppercase A B C D E F)

Escape sequences

\n

New line

\t

Horizontal tab

\v

Vertical tab

\?

Question mark

\'

Single quote

\"

Double quote

\\

Backslash

\b

Backspace

Directives

#include

Includes foreign file's methods

#include <header>

#include "stdio.h"

#define

Defines a constant

#define PI 3.14159265315

Defines a lambda expression

#define max(A, B) ((A) > (B) ? (A) : (B))

Commenting

//

Used to tell compiler the rest of this line is not to be compiled

/*

Opens a multi line comment

*/

Closes a multi line comment

Compiler tags

Wall

Wextra