Show Menu

C++ Cheatsheet(inc. C++2011) Cheat Sheet by

cpp     programming     intermediate     advanced     c     c-0x

Standard Sytemtypes and their range

int8_t
-128 to 127
uint8_t
0 to 255
int16_t
-32768 to 32767
uint16_t
0 to 65535
int32_t
-21474­83648 to 2147483647
uint32_t
0 to 4294967295
int64_t
-9.2 * 1018 to 9.2 * 1018
uint64_t
0 to 1.8 * 1019

Literals & Co.

255
Integer
0xaf
Hexade­cimal Integer
1234.0
double
234.243f
float
true
bool
"­Hello World"
string­/c-­string
'a'
char

Prepro­cessor

#include <LI­B>
Includes standard header file LIB
#include "­Hea­der.h"
Includes Header.h file
#define PI 3.1415­9265359
Defines Easy Macro
#define f(a,b) a+b
More Complex Macro
#undef PI
Remove Definition
#ifdef PI
Checks for defini­tion, if true, code will be compiled
#else
Else part of the above if-sta­tement
#endif
Ends the if-sta­tement

Nullpo­inter

int* pInt;
//old, deprec­ated:
if(pInt == NULL)
//new:
if(pInt == nullptr)
//still valid:
if(!pInt)
 

Bitwise Operators

&
Bitwise AND
|
Bitwise OR
^
Bitwise XOR
~
Bitwise NOT
<<
Shift left
>>
Shift right

Boolean Logic

==
Test of equality
!=
Test of non-eq­uality
<
Less than
>
Greater than
<=
Less than or equal
>=
Greater than or equal
!
NOT
&&
AND
||
OR
Boolean expres­sions in C++ are evaluated left-t­o-r­ight!

Basic Operators

+
addition
-
subtra­ction
*
multip­lic­ation
/
division
%
modulo
++var
increase before evaluation
var++
increase after evaluation
condition ? result : altern­ative;
short form of if-like structure
::
Scope Operator
->
pointe­r-t­o-m­ember
.
Access member
<< >>
Bitshi­ft(with streams: input/­output)

Pointers: Quick and dirty

&
Gets RAM adress of Variab­le(to save into pointer)
*
Derefe­rences pointe­r(r­eturns it´s content) or defines a variable to be a pointer
->
Access pointer class member. same as (*poin­ter­).m­ember()
new
Create new object on heap, returns pointer to object
delete
Remove object at the pointer on heap
Pointers allocate space on heap, normal variables on stack!
 

using replaces typedef

//typedef is deprecated

typedef uint32_t uint32;

//now: using directive!
//using identifier = type_name;

using uint32 = uint32_t;

Auto Datatype

//auto is an automatic datatype:
int x = 4; //equals:
auto y = 4;

//works for most cases, esp. STL:
for(st­d::­vec­tor­<in­t>:­:It­erator it = v.begi­n().....)
//with auto:
for(auto it = v.begin; it != v.end(); ++it)

Compil­e-time assertion check

static assert­(bo­ol_­con­stexpr, string)

Multit­hre­ading

//Thread Creation & Manage­ment:
void thread­_fu­nc(int a)
{
std::cout << "My Number is: " << a;
}
main()
{
std::t­hread t1(thr­ead­_fu­nc(1));
t1.join();
}
//Sync­hro­niz­ation via:
std::m­ute­x.l­ock();
std::m­ute­x.u­nlo­ck();

Range based loops

//Easy range based loop:
std::v­ect­or<­int> vec = .....;
for(int i : vec) //foreach i in vec

//Same with auto:
const auto vi = ....;
for(auto i : vi)
Works with all Containers with begin() and end()

Download the C++ Cheatsheet(inc. C++2011) Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/leupi_c-cheatsheet-inc-c-2011.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

John Doe VI John Doe VI, 13:28 24 Feb 16

Very basic and straightforward. Would have preferred to have the comments placed to the right instead of above and readjust other parts to the second page to accommodate proper spacing.
Thanks.

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          oAuth End Points Cheat Sheet
          Object Oriented Design Cheat Sheet
          C Reference Cheat Sheet