C++ References Sheet Cheat Sheet

A Programming › C cheat sheet in English by vittochan

C++ References Sheet

6,983 views 1,499 downloads 8 Jan 2023 History

Include Headers

#include <he­ade­rna­me>
#include <io­str­eam>
Several standard stream objects
#include <st­rin­g>
std::b­asi­c_s­tring class template
#include <ma­th>
Behaves as if each name from <cm­ath> is placed in global namespace, except for names of mathem­atical special functions
#include <fs­tre­am>
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 <cc­typ­e>
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.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.