Quickref Me CPP
Quickref Me CPP
C++ cheatsheet
C++ quick reference cheat sheet that provides basic syntax and methods.
# Getting started
hello.cpp Variables Primitive Data Types
int num; int a = 5, b = 10, temp; // A single one line comment in C++
temp = a;
std::cout << "Type a number: "; a = b; /* This is a multiple line comment
std::cin >> num; b = temp; in C++ */
std::cout << "You entered " << num; // Outputs: a=10, b=5
std::cout << "a=" << a << ", b=" << b;
References Namespaces
ri and i refer to the same memory location. Namespaces allow global identifiers under a name
# C++ Arrays
Declaration Manipulation Displaying
Multidimensional
j0 j1 j2 j3 j4 j5
┌────┬────┬────┬────┬────┬────┐
i0 | 1 | 2 | 3 | 4 | 5 | 6 |
├────┼────┼────┼────┼────┼────┤
i1 | 6 | 5 | 4 | 3 | 2 | 1 |
└────┴────┴────┴────┴────┴────┘
int x[2][6] = {
{1,2,3,4,5,6}, {6,5,4,3,2,1}
};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 6; ++j) {
std::cout << x[i][j] << " ";
}
}
// Outputs: 1 2 3 4 5 6 6 5 4 3 2 1
# C++ Conditionals
If Clause Else if Statement _ Operators
Relational Operators
if (a == 10) { int score = 99;
// do something if (score == 100) { a == b a is equal to b
} std::cout << "Superb";
} a != b a is NOT equal to b
else if (score >= 90) {
int number = 16;
std::cout << "Excellent"; a < b a is less than b
}
if (number % 2 == 0)
else if (score >= 80) { a > b a is greater b
{
std::cout << "Very Good";
std::cout << "even"; a <= b a is less than or equal to b
}
}
else if (score >= 70) {
else a >= b a is greater or equal to b
std::cout << "Good";
{
} Assignment Operators
std::cout << "odd";
else if (score >= 60)
}
std::cout << "OK"; a += b Aka a = a + b
else
// Outputs: even a -= b Aka a = a - b
std::cout << "What?";
a *= b Aka a = a * b
a /= b Aka a = a / b
Ternary Operator Switch Statement
a %= b Aka a = a % b
┌── True ──┐ int num = 2;
Result = Condition ? Exp1 : Exp2; switch (num) { Logical Operators
└───── False ─────┘ case 0:
std::cout << "Zero";
exp1 && exp2 Both are true (AND)
break;
int x = 3, y = 5, max; exp1 || exp2 Either is true (OR)
case 1:
max = (x > y) ? x : y;
std::cout << "One";
!exp exp is false (NOT)
break;
// Outputs: 5
case 2: Bitwise Operators
std::cout << max << std::endl;
std::cout << "Two";
std::cout << Two ;
break; a & b Binary AND
int x = 3, y = 5, max; case 3:
a | b Binary OR
if (x > y) { std::cout << "Three";
max = x; break;
a ^ b Binary XOR
} else { default:
max = y; std::cout << "What?";
a ~ b Binary One's Complement
} break;
// Outputs: 5 } a << b Binary Shift Left
std::cout << max << std::endl;
a >> b Binary Shift Right
# C++ Loops
While Do-while Continue statements
# C++ Functions
Arguments & Returns Overloading Built-in Functions
# C++ Preprocessor
Preprocessor Includes Defines
define undef
If Error
include line
#ifdef DEBUG #if VERSION == 2.0
console.log('hi'); #error Unsupported
error pragma
#elif defined VERBOSE #warning Not really supported
defined __has_include ... #endif
#else
__has_cpp_attribute export ...
#endif Macro
import module
#define DEG(x) ((x) * 57.29)
#define DST(name) name##_s name##_t #define STR(name) #name #define LOG(msg) console.log(__FILE__, __LI
DST(object); #=> object_s object_t; char * a = STR(object); #=> char * a = "o #=> console.log("file.txt", 3, "hey")
# Miscellaneous
Escape Sequences Keywords
import module
# Also see
C++ Infographics & Cheat Sheets (hackingcpp.com)
C++ reference (cppreference.com)
C++ Language Tutorials (cplusplus.com)
#Notes
Bash Cheatsheet JavaScript Cheatsheet GraphQL Cheatsheet Chmod Cheatsheet
Quick Reference Quick Reference Quick Reference Quick Reference