Show Menu
Cheatography

C++ References Sheet Cheat Sheet by

C++ References Sheet

Include Headers

#include <headername>
#include <iostream>
Several standard stream objects
#include <string>
std::b­asi­c_s­tring class template
#include <math>
Behaves as if each name from <cm­ath> is placed in global namespace, except for names of mathem­atical special functions
#include <fstream>
std::b­asi­c_f­stream, std::b­asi­c_i­fst­ream, std::b­asi­c_o­fstream class templates and several typedefs
#include <cctype>
Behaves as if each name from <cc­typ­e> is placed in global namespace

Namespace

using namespace std;

Data Types

int
integer type (-32768 to 32767)
char
single character type
float
Floati­ng-­point types (single precision)
double
Floati­ng-­point types (double precision)
bool
true or false
void
type with an empty set of values

Comments

//Single line
/*Multiple
lines*/

Pointers

int *ptr;
Pointer definition
ptr = &var1;
ptr set to address of var1
var2 = *ptr;
set var2 to value of var1
 

Arithmetic Operators

+, -, *, /
Addition, subtra­ction, multip­lic­ation, division
%
Modulus (rest of division)
++
Add 1 to variable (
example:
i++)
--
Subtract 1 to variable (
example:
i--)
+=, -=, *=, /=
Add/su­btr­act­/mu­lti­ply­/divide a value from the variable (
example:
i+=2; is equal to i=i+2;)

Relational Operators

<
Less than
<=
Less or Equal than
>
Greater than
>=
Greater or Equal than
==
Equal than
!=
Not Equal than

Logical Operators

||
OR
&&
AND
!
NOT

If Else

if (condition) {
 ­ ­ ­ \\Do something
} else if (condition) {
 ­ ­ ­ \\Do something
} else {
 ­ ­ ­ \\Do something
}
condition must be a condition that return a boolean value true or false
else/else if blocks are optional
 

Switch

switch (variable) {
case constant1:
 ­ ­ ­ \\Do something
break;
case constant2:
 ­ ­ ­ \\Do something
break;
default:
 ­ ­ ­ \\Do something
break;
}
default is optional

While Loop

while (condition) {
 ­ ­ ­ \\Do something
}
Loop as long as the condition is true, when it becomes false it exits

Do-While Loop

do {
 ­ ­ ­ \\Do something
} while (condition);
Loop as long as the condition is true, when it becomes false it exits

For Loop

for (initialize; condition; update) {
 ­ ­ ­ \\Do something
}

example:

for (int i=0; i<10; i++) {
 ­ ­ ­ \\Do something
}
Loop as long as the condition is true, when it becomes false it exits
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Numeric Formats Cheat Sheet
          C# & Unity MonoBehaviour Cheat Sheet

          More Cheat Sheets by vittochan

          Remote Desktop Keyboard Shortcuts