C++
Hello World
#include <iostream> // For using cout
#include <cstdio> // For using printf
int main() {
std::cout << "Hello, World!" << std::endl;
printf("Hello, World!\n");
return 0;
}
Data Types - Primary
int // Integer (whole number)
float // Floating point number
double // Floating point number
char // Character
bool // Boolean: true or false
string // String
void // Empty
wchar_t // Wide character containing 2 bits
// Modifiers applicable to int, double and char
signed int // 4 bytes, used for integers (equivalent to int)
unsigned int // 4 bytes, can only store positive integers
short // 2 bytes, used for small integers (range -32768 to 32767)
unsigned short // 2 bytes, used for small positive integers (range 0 to 65,535)
long // at least 4 bytes, used for large integers (equivalent to long int)
unsigned long // 4 bytes,used for large positive integers or 0 (equivalent to unsigned long int)
long long // 8 bytes,used for very large integers (equivalent to long long int).
unsigned long long // 8 bytes, used for very large positive integers or 0 (equivalent to unsigned long long int)
long double // 12 bytes, used for large floating-point numbers
signed char // 1 byte, used for characters (guaranteed range -127 to 127)
unsigned char // 1 byte, used for characters (range 0 to 255)
// Scope
static int x; // Global lifetime even if local scope
extern int x; // Information only, declared elsewhere
Data Types - Derived
Array
int a[n]; // Array of n ints (a[0] through a[n-1])
int a[]={0,1,2}; // Initialized array (or a[3]={0,1,2}; )
int a[n][m]; // Array of array of ints
char s[]="hello"; // String (6 elements including '\0')
string cars[4] = {"Volvo", "BMW", "Ford", "Audi"}; // Array of strings
Pointer
int* p; // p is a pointer to (address of) int
float* px = 1.1; // x is a pointer to (address of) float 1.1
char* s="hello"; // s points to unnamed array containing "hello"
void* p=nullptr; // Address of untyped memory (nullptr is 0)
Reference
int& r = x; // r is a reference to (alias of) int x
Data Types - User Defined
Structure
#include <cstdio> // For using printf
struct Student
{
char * name;
char * major;
int age;
};
int main() {
Student student1 = { "John", "Engineering" , 20 };
printf("Hello, %10s. You are %i years old. Your major: %21s.\n", student1.name, student1.age, student1.major);
Student student2;
student2.name = "Mary";
student2.major = "Physics";
student2.age = 21;
printf("Hello, %s. You are %i years old. Your major: %s.\n", student2.name, student2.age, student2.major);
return 0;
}
Class
#include <iostream>
class Employee {
int id;
char * name;
float salary;
public:
void insert(int i, char * n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
printf("Employee id=%i, called %s, annual salary of USD %9.2f.\n", id, name, salary);
}
};
int main() {
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
e1.insert(201, "John", 100000);
e2.insert(102, "Mary",120000);
e1.display();
e2.display();
return 0;
}
All classes have a default copy constructor, assignment operator, and destructor, which perform the corresponding operations on each data member and each base class as shown above. There is also a default no-argument constructor (required to create arrays) if the class has no constructors. Constructors, assignment, and destructors do not inherit.
class T { // A new type
private: // Section accessible only to T's member functions
protected: // Also accessible to classes derived from T
public: // Accessible to all
int x; // Member data
void f(); // Member function
void g() {return;} // Inline member function
void h() const; // Does not modify any data members
int operator+(int y); // t+y means t.operator+(y)
int operator-(); // -t means t.operator-()
T(): x(1) {} // Constructor with initialization list
T(const T& t): x(t.x) {}// Copy constructor
T& operator=(const T& t)
{x=t.x; return *this; } // Assignment operator
~T(); // Destructor (automatic cleanup routine)
explicit T(int a); // Allow t=T(3) but not t=3
T(float x): T((int)x) {}// Delegate constructor to T(int)
operator int() const
{return x;} // Allows int(t)
friend void i(); // Global function i() has private access
friend class U; // Members of class U have private access
static int y; // Data shared by all T objects
static void l(); // Shared code. May access y but not x
class Z {}; // Nested class T::Z
typedef int V; // T::V means int
};
void T::f() { // Code for member function f of class T
this->x = x;} // this is address of self (means x=x;)
int T::y = 2; // Initialization of static member (required)
T::l(); // Call to static member
T t; // Create object t implicit call constructor
t.f(); // Call method f on object t
struct T { // Equivalent to: class T { public:
virtual void i(); // May be overridden at run time by derived class
virtual void g()=0; }; // Must be overridden (pure virtual)
class U: public T { // Derived class U inherits all members of base T
public:
void g(int) override; }; // Override method g
class V: private T {}; // Inherited members of T become private
class W: public T, public U {};
// Multiple inheritance
class X: public virtual T {};
// Classes derived from X have base T directly
Union
The purpose of union is to save memory by using the same memory region for storing different objects at different times.
#include <cstdio>
#include <cstdint>
union ipv4 {
uint32_t i32;
struct {
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
} octets;
};
int main()
{
union ipv4 addr;
addr.octets = { 192, 168, 73, 42 };
printf("addr is %d.%d.%d.%d is %08x\n",
addr.octets.a, addr.octets.b, addr.octets.c, addr.octets.d, addr.i32 );
return 0;
}
Enum
Enums are user-defined types that consist of named integral constants for improving readability.
#include <iostream>
enum week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
int main()
{
week day;
day = Friday;
printf("Day: %i\n", day+1);
return 0;
}
#include <iostream>
enum card_suit : int { SPD, HRT, DIA, CLB };
enum card_rank : int { ACE = 1, DEUCE = 2, JACK = 11, QUEEN, KING };
const char * aceString = "Ace";
const char * jckString = "Jack";
const char * queString = "Queen";
const char * kngString = "King";
const char * deuString = "Deuce";
const char * spdString = "Spades";
const char * hrtString = "Hearts";
const char * diaString = "Diamonds";
const char * clbString = "Clubs";
int main() {
card_rank rank = JACK;
card_suit suit = SPD;
switch(rank) {
case ACE:
printf("%s of ", aceString);
break;
case JACK:
printf("%s of ", jckString);
break;
case QUEEN:
printf("%s of ", queString);
break;
case KING:
printf("%s of ", kngString);
break;
case DEUCE:
printf("%s of ", deuString);
break;
}
switch(suit) {
case SPD:
puts(spdString);
break;
case HRT:
puts(hrtString);
break;
case DIA:
puts(diaString);
break;
case CLB:
puts(clbString);
break;
}
return 0;
}
Typedef
#include <cstdio>
typedef unsigned char points_t;
typedef unsigned char rank_t;
struct score {
points_t p;
rank_t r;
};
int main()
{
score s = { 5, 1 };
printf("score s had %d points and a rank of %d\n", s.p, s.r);
return 0;
}
Declarations
int x; // Declare x to be an integer (value undefined). Auto: memory exists only while in scope)
int x=255; // Declare and initialize x to 255
int a, b, c; // Multiple declarations
std::string s = "Hello" // Creates string object with value "Hello"
std::string s = R"(Hello World)"; // Creates string object with value "Hello\nWorld"
enum weekend {SAT,SUN}; // weekend is a type with values SAT and SUN
enum weekend day; // day is a variable of type weekend
enum weekend{SAT=0,SUN=1}; // Explicit representation as int
enum {SAT,SUN} day; // Anonymous enum
enum class Color {Red,Blue};// Color is a strict type with values Red and Blue
Color x = Color::Red; // Assign Color x to red
typedef String char*; // String s; means char* s;
const int c=3; // Constants must be initialized, cannot assign to
const int* p=a; // Contents of p (elements of a) are constant
int* const p=a; // p (but not contents) are constant
const int* const p=a; // Both p and its contents are constant
const int& cr=x; // cr cannot be assigned to change x
int8_t,uint8_t,int16_t,
uint16_t,int32_t,uint32_t,
int64_t,uint64_t // Fixed length standard types
auto it = m.begin(); // Declares it to the result of m.begin()
auto const param = config["param"];
// Declares it to the const result
auto& s = singleton::instance();
// Declares it to a reference of the result
Statements
x=y; // Every expression is a statement
int x; // Declarations are statements
; // Empty statement
{ // A block is a single statement
int x; // Scope of x is from declaration to end of block
}
'\n', '\\', '\'', '\"' // Newline, backslash, single quote, double quote
Expressions
Operators are grouped by precedence, highest first. Unary operators and assignment evaluate right to left. All others are left to right. Precedence does not affect order of evaluation, which is undefined. There are no run time checks for arrays out of bounds, invalid pointers, etc.
T::X // Name X defined in class T
N::X // Name X defined in namespace N
::X // Global name X
t.x // Member x of struct or class t
p-> x // Member x of struct or class pointed to by p
a[i] // i'th element of array a
f(x,y) // Call to function f with arguments x and y
T(x,y) // Object of class T initialized with x and y
x++ // Add 1 to x, evaluates to original x (postfix)
x-- // Subtract 1 from x, evaluates to original x
typeid(x) // Type of x
typeid(T) // Equals typeid(x) if x is a T
dynamic_cast< T>(x) // Converts x to a T, checked at run time.
static_cast< T>(x) // Converts x to a T, not checked
reinterpret_cast< T>(x) // Interpret bits of x as a T
const_cast< T>(x) // Converts x to same type T but not const
sizeof x // Number of bytes used to represent object x
sizeof(T) // Number of bytes to represent type T
++x // Add 1 to x, evaluates to new value (prefix)
--x // Subtract 1 from x, evaluates to new value
~x // Bitwise complement of x
!x // true if x is 0, else false (1 or 0 in C)
-x // Unary minus
+x // Unary plus (default)
&x // Address of x
*p // Contents of address p (*&x equals x)
new T // Address of newly allocated T object
new T(x, y) // Address of a T initialized with x, y
new T[x] // Address of allocated n-element array of T
delete p // Destroy and free object at address p
delete[] p // Destroy and free array of objects at p
(T) x // Convert x to T (obsolete, use .._cast<T>(x))
x * y // Multiply
x / y // Divide (integers round toward 0)
x % y // Modulo (result has sign of x)
x + y // Add, or \&x[y]
x - y // Subtract, or number of elements from *x to *y
x << y // x shifted y bits to left (x * pow(2, y))
x >> y // x shifted y bits to right (x / pow(2, y))
x < y // Less than
x <= y // Less than or equal to
x > y // Greater than
x >= y // Greater than or equal to
x & y // Bitwise and (3 & 6 is 2)
x ^ y // Bitwise exclusive or (3 ^ 6 is 5)
x | y // Bitwise or (3 | 6 is 7)
x && y // x and then y (evaluates y only if x (not 0))
x || y // x or else y (evaluates y only if x is false (0))
x = y // Assign y to x, returns new value of x
x += y // x = x + y, also -= *= /= <<= >>= &= |= ^=
x ? y : z // y if x is true (nonzero), else z
throw x // Throw exception, aborts if not caught
x , y // evaluates x and y, returns y (seldom used)
Flow Control
If
if ( expression_x ){
a;
}
else if ( expression_y ){ // optional, may be repeated
b;
}
else { // optional
c;
}
For
for (int i = 1; i <= 10 ; i++) {
printf("%d\n",i);
}
// Range based loop
int a[] = {0, 1, 2, 3, 4, 5};
for (int n : a){
printf("%d\n",n);
}
While
while ( expression_x ){
a;
}
do {
a;
}
while ( expression_x );
Switch
switch (x) { // x must be int
case X1: a; // If x == X1 (must be a const), jump here
case X2: b; // Else if x == X2, jump here
default: c; // Else jump here (optional)
}
Exceptions
try { // Block of code to try
a;
throw exception; // Throw an exception when a problem arise
}
catch ( T t) { // Block of code to be executed, if an error T is thrown from the try block.
b;
}
catch (...) { // Block of code to be executed, if an error something else is thrown from the try block.
c;
}
Auxiliary
break; // Jump out of while, do, or for loop, or switch
continue; // Jump to bottom of while, do, or for loop
return x; // Return x from function to caller
try { a; }
catch (T t) { b; } // If a throws a T, then jump here
catch (...) { c; } // If a throws something else, jump here
Functions
Every program must include the main function, which is automatically called when the program is executed. By convention, main
returns status 0
if successful, 1
or higher for errors.
int main() {
statements...
}
int main(int argc, char* argv[]) { // argv is an array of argc strings from the command line
statements...
}
#include <cstdio>
// Declares and defines a function
void myFunction() {
printf("Hello, World!");
}
int main() {
myFunction(); // call the function
return 0;
}
A function must either be declared or defined before it is used. It may be declared first and defined later in the same file.
#include <cstdio>
// Declares function myFunction
void myFunction()
int main() {
myFunction(); // call the function
return 0;
}
// Defines function myFunction
void myFunction() {
printf("Hello, World!");
}
Functions can also be defined in other files.
#include <cstdio>
#include "src/funcs.h"
int main() {
myFunction(); // call the function
return 0;
}
where file funcs.h
is inside folder src
:
#ifndef FUNCS_H_
#define FUNCS_H_
// Defines function myFunction
void myFunction() {
printf("Hello, World!");
}
#endif //FUNCS_H_
Function parameters and return values may be of any type.
int f(int x, int y); // f is a function taking 2 ints and returning int
void f(); // f is a procedure taking no arguments
void f(int a=0); // f() is equivalent to f(0)
f() { statements; } // Function definition (must be global)
inline f(); // Optimize for speed
T operator+(T x, T y); // a+b (if type T) calls operator+(a, b)
T operator-(T x); // -a calls function operator-(a)
T operator++(int); // postfix ++ or -- (parameter ignored)
extern "C" {void f();} // f() was compiled in C
Overloading
Functions with different parameters may have the same name (overloading). Operators except ::
.
.*
?:
may be overloaded.
Precedence order is not affected. New operators may not be created.
Templates
template <class T> T f(T t);// Overload f for all types
template <class T> class X {// Class with type parameter T
X(T t); }; // A constructor
template <class T> X<T>::X(T t) {}
// Definition of constructor
X<int> x(3); // An object of type "X of int"
template <class T, class U=T, int n=0>
// Template with default parameters
Data Structures and STL Templates
String
Variable sized character array.
#include <cstring> // Include string (std namespace)
string s1, s2="hello"; // Create strings
s1.size(), s2.size(); // Number of characters: 0, 5
s1 += s2 + ' ' + "world"; // Concatenation
s1 == "hello world" // Comparison, also <, >, !=, etc.
s1[0]; // 'h'
s1.substr(m, n); // Substring of size n starting at s1[m]
s1.c_str(); // Convert to const char*
s1 = to_string(12.05); // Converts number to string
getline(cin, s); // Read line ending in '\n'
Vector
Variable sized array/stack with built in memory allocation.
#include <vector> // Include vector (std namespace)
vector<int> a(10); // a[0]..a[9] are int (default size is 0)
vector<int> b{1,2,3}; // Create vector with values 1,2,3
a.size(); // Number of elements (10)
a.push_back(3); // Increase size to 11, a[10]=3
a.back()=4; // a[10]=4;
a.pop_back(); // Decrease size by 1
a.front(); // a[0];
a[20]=1; // Crash: not bounds checked
a.at(20)=1; // Like a[20] but throws out_of_range()
for (int& p : a)
p=0; // C++11: Set all elements of a to 0
for (vector<int>::iterator p=a.begin(); p!=a.end(); ++p)
*p=0; // C++03: Set all elements of a to 0
vector<int> b(a.begin(), a.end()); // b is copy of a
vector<T> c(n, x); // c[0]..c[n-1] init to x
T d[10]; vector<T> e(d, d+10); // e is initialized from d
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// utility functions
// print the elements of the vector
template<typename T>
void printv(vector<T> & v) {
if(v.empty()) return;
for(T &i : v) cout << i << " ";
cout << endl;
}
// print a simple message
void message(const char * s) { cout << s << endl; }
void message(const char * s, const int n) { cout << s << ": " << n << endl; }
// MARK: - main
int main() {
cout << "vector from initializer list: " << endl;
vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
printv(v1);
// info
message("size", (int) v1.size());
message("front", v1.front());
message("back", v1.back());
// index
message("element at 5", v1[5]);
message("element at 5", v1.at(5));
// insert
message("insert 42 at begin + 5:");
v1.insert(v1.begin() + 5, 42);
printv(v1);
// erase
message("erase at begin + 5:");
v1.erase(v1.begin() + 5);
printv(v1);
// push_back
message("push back 47:");
v1.push_back(47);
printv(v1);
// pop_back
message("pop_back:");
v1.pop_back();
printv(v1);
// empty
message("empty:");
vector<int> vx = { 1, 2, 3 };
while(!vx.empty()) {
printv(vx);
vx.pop_back();
}
// clear
message("clear:");
vx.insert(vx.begin(), { 1, 2, 3, 4, 5 });
printv(vx);
message("vx size", (int) vx.size());
message("vx.clear()");
vx.clear();
message("vx size", (int) vx.size());
printv(vx);
// constructors
// from C-array
constexpr size_t size = 10;
int ia[size] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
message("vector from C-array:");
vector<int> v2(ia, ia + size);
printv(v2);
// filled with strings
message("vector filled with string:");
vector<string> v3(5, "string");
printv(v3);
// copy constructor
message("vector copied from vector:");
vector<string> v4(v3);
printv(v4);
// move constructor
message("vector (v5) moved from vector (v4):");
message("size of v4", (int) v4.size());
vector<string> v5(std::move(v4));
printv(v5);
message("size of v4", (int) v4.size());
return 0;
}
Deque
Array stack queue.
#include <deque> // Include deque (std namespace)
a.push_front(x); // Puts x at a[0], shifts elements toward back
a.pop_front(); // Removes a[0], shifts toward front
Pair
#include <utility> // Include utility (std namespace)
pair<string, int> a("hello", 3); // A 2-element struct
a.first; // "hello"
a.second; // 3
Map
Associative array, usually implemented as binary search tree (complexity O(log n)).
#include <map> // Include map (std namespace)
map<string, int> a; // Map from string to int
a["hello"] = 3; // Add or replace element a["hello"]
for (auto& p:a)
cout << p.first << p.second; // Prints hello, 3
a.size(); // 1
Unordered Map
Associative array, usually implemented as hash table (complexity O(1)).
#include <unordered_map> // Include map (std namespace)
unordered_map<string, int> a; // Map from string to int
a["hello"] = 3; // Add or replace element a["hello"]
for (auto& p:a)
cout << p.first << p.second; // Prints hello, 3
a.size(); // 1
Set
A set stores unique elements and is usually implemented as binary search trees (complexity O(log n)).
#include <set> // Include set (std namespace)
set<int> s; // Set of integers
s.insert(123); // Add element to set
if (s.find(123) != s.end()) // Search for an element
s.erase(123);
cout << s.size(); // Number of elements in set
Unordered Set
A set stores unique elements and is usually implemented as a hash set (complexity O(1)).
#include <unordered_set> // Include set (std namespace)
unordered_set<int> s; // Set of integers
s.insert(123); // Add element to set
if (s.find(123) != s.end()) // Search for an element
s.erase(123);
cout << s.size(); // Number of elements in set
Preprocessor
// Comments to end of line
/* Multi-line
comment */
#include <cstdio> // Inserts standard header file
#include <chrono> // (C++11) C++ time utilites
#include <cstdarg> // Handling of variable length argument lists
#include <cstddef> // Standard macros and typedefs
#include <cstdlib> // General purpose utilities: program control, dynamic memory allocation, random numbers, sort and search
#include <cstring> // Various narrow character string handling functions
#include <array> // (C++11) std::array container
#include <deque> // std::deque container
#include <forward_list> // (C++11) std::forward_list container
#include <list> // std::list container
#include <map> // std::map and std::multimap associative containers
#include <queue> // std::queue and std::priority_queue container adaptors
#include <set> // std::set and std::multiset associative containers
#include <span> // (C++20) std::span view
#include <stack> // std::stack container adaptor
#include <unordered_map> // (C++11) std::unordered_map and std::unordered_multimap unordered associative containers
#include <unordered_set> // (C++11) std::unordered_set and std::unordered_multiset unordered associative containers
#include <vector> // std::vector container
#include <iterator> // Range iterators
#include <ranges> // (C++20) Range access, primitives, requirements, utilities and adaptors
#include <iostream> // Allows the usage of input and output objects
#include <fstream> // Allows both read and write from/to files.
#include <cmath> // Common mathematics functions
#include <complex> // Complex number type
#include <numbers> // (C++20) Math constants
#include <numeric> // Numeric operations on values in ranges
#include <random> // (C++11) Random number generators and distributions
#include <regex> // (C++11) Classes, algorithms and iterators to support regular expression processing
using namespace std; // Allows the direct usage of the names of objects and variables from the standard library
#include "myfile.h" // Inserts file in current directory
#define X "some text" // Replaces X with some text
#define F(a,b) a+b // Replaces F(1,2) with 1+2
#define PI 3.14159265 // Defines a constant
#define X \
some text // Multiline definition
#undef X // Remove definition
#if defined(X) // Conditional compilation (#ifdef X)
#else // Optional (#ifndef X or #if !defined(X))
#endif // Required after #if, #ifdef
Namespaces
namespace N {class T {};} // Hide name T
N::T t; // Use name T in namespace N
using namespace N; // Make T visible without N::
Hearder File Libraries
<memory>
: Dynamic Memory Management
#include <memory> // Include memory (std namespace)
shared_ptr<int> x; // Empty shared_ptr to a integer on heap. Uses reference counting for cleaning up objects.
x = make_shared<int>(12); // Allocate value 12 on heap
shared_ptr<int> y = x; // Copy shared_ptr, implicit changes reference count to 2.
cout << *y; // Dereference y to print '12'
if (y.get() == x.get()) { // Raw pointers (here x == y)
cout << "Same";
}
y.reset(); // Eliminate one owner of object
if (y.get() != x.get()) {
cout << "Different";
}
if (y == nullptr) { // Can compare against nullptr (here returns true)
cout << "Empty";
}
y = make_shared<int>(15); // Assign new value
cout << *y; // Dereference x to print '15'
cout << *x; // Dereference x to print '12'
weak_ptr<int> w; // Create empty weak pointer
w = y; // w has weak reference to y.
if (shared_ptr<int> s = w.lock()) { // Has to be copied into a shared_ptr before usage
cout << *s;
}
unique_ptr<int> z; // Create empty unique pointers
unique_ptr<int> q;
z = make_unique<int>(16); // Allocate int (16) on heap. Only one reference allowed.
q = move(z); // Move reference from z to q.
if (z == nullptr){
cout << "Z null";
}
cout << *q;
shared_ptr<B> r;
r = dynamic_pointer_cast<B>(t); // Converts t to a shared_ptr<B>
<cmath.h>
: Floating Point Math
#include <cmath> // Include cmath (std namespace)
sin(x); cos(x); tan(x); // Trig functions, x (double) is in radians
asin(x); acos(x); atan(x); // Inverses
atan2(y, x); // atan(y/x)
sinh(x); cosh(x); tanh(x); // Hyperbolic sin, cos, tan functions
exp(x); log(x); log10(x); // e to the x, log base e, log base 10
pow(x, y); sqrt(x); // x to the y, square root
ceil(x); floor(x); // Round up or down (as a double)
fabs(x); fmod(x, y); // Absolute value, x mod y
<cassert.h>
: Debugging Aid
#include <cassert> // Include iostream (std namespace)
assert(e); // If e is false, print message and abort
#define NDEBUG // (before #include <assert.h>), turn off assert
<iostream>
#include <iostream> // Include iostream (std namespace)
cin >> x >> y; // Read words x and y (any type) from stdin
cout << "x=" << 3 << endl; // Write line to stdout
cerr << x << y << flush; // Write to stderr and flush
c = cin.get(); // c = getchar();
cin.get(c); // Read char
cin.getline(s, n, '\n'); // Read line into char s[n] to '\n' (default)
if (cin) // Good state (not EOF)?
// To read/write any type T:
istream& operator>>(istream& i, T& x) {i >> ...; x=...; return i;}
ostream& operator<<(ostream& o, const T& x) {return o << ...;}
<fstream>
: File I/O
#include <fstream> // Include filestream (std namespace)
ifstream f1("filename"); // Open text file for reading
if (f1) // Test if open and input available
f1 >> x; // Read object from file
f1.get(s); // Read char or line
f1.getline(s, n); // Read line into string s[n]
ofstream f2("filename"); // Open file for writing
if (f2) f2 << x; // Write to file
<algorithm>
: A Collection of 60 Algorithms on Sequences with Iterators
#include <algorithm> // Include algorithm (std namespace)
min(x, y); max(x, y); // Smaller/larger of x, y (any type defining <)
swap(x, y); // Exchange values of variables x and y
sort(a, a+n); // Sort array a[0]..a[n-1] by <
sort(a.begin(), a.end()); // Sort vector or deque
reverse(a.begin(), a.end()); // Reverse vector or deque
<chrono>
: Time Related Library
#include <chrono> // Include chrono
using namespace std::chrono; // Use namespace
auto from = // Get current time_point
high_resolution_clock::now();
// ... do some work
auto to = // Get current time_point
high_resolution_clock::now();
using ms = // Define ms as floating point duration
duration<float, milliseconds::period>;
// Compute duration in milliseconds
cout << duration_cast<ms>(to - from)
.count() << "ms";
<thread>
: Multi-Threading Library
#include <thread> // Include thread
unsigned c =
hardware_concurrency(); // Hardware threads (or 0 for unknown)
auto lambdaFn = [](){ // Lambda function used for thread body
cout << "Hello multithreading";
};
thread t(lambdaFn); // Create and run thread with lambda
t.join(); // Wait for t finishes
// --- shared resource example ---
mutex mut; // Mutex for synchronization
condition_variable cond; // Shared condition variable
const char* sharedMes // Shared resource
= nullptr;
auto pingPongFn = // thread body (lambda). Print someone else's message
[&](const char* mes){
while (true){
unique_lock<mutex> lock(mut);// locks the mutex
do {
cond.wait(lock, [&](){ // wait for condition to be true (unlocks while waiting which allows other threads to modify)
return sharedMes != mes; // statement for when to continue
});
} while (sharedMes == mes); // prevents spurious wakeup
cout << sharedMes << endl;
sharedMes = mes;
lock.unlock(); // no need to have lock on notify
cond.notify_all(); // notify all condition has changed
}
};
sharedMes = "ping";
thread t1(pingPongFn, sharedMes); // start example with 3 concurrent threads
thread t2(pingPongFn, "pong");
thread t3(pingPongFn, "boing");
<future>
: Thread Support Library
#include <future> // Include future
function<int(int)> fib = // Create lambda function
[&](int i){
if (i <= 1){
return 1;
}
return fib(i-1)
+ fib(i-2);
};
future<int> fut = // result of async function
async(launch::async, fib, 4); // start async function in other thread
// do some other work
cout << fut.get(); // get result of async function. Wait if needed.