C++ Cheat Sheet

1000 worst ways to do the same thing

#In & Output

Hello World!

#include <iostream>:
using namespace std;

int main(int argc, const char * argv[]) {
    cout << "Hello World!";
}
// with a line break:
cout << "Hello World!\n";
// generates the same result:
cout << "Hello World!" << endl;
Important: you need to include #iostream.

Combining Outputs

cout << "First " << "Second " << "Third";
cout << "First" << endl << "Second";
// printing the return-value of a function:
int add(int x) {
    return x+x;
}

int main(int argc, const char * argv[]) {
    cout << add(2); // 4
}

Getting User Input

std::cout << "Enter string:" << endl;
std::string s;
std::getline(std::cin, s);
std::cout << "the string was: " << s << std::endl;

#Functions

Return-Statement

int add(int x) {
    return x;
}
You can only return one value.

Multiple Parameters

int add(int x, int y) {
    return x+y;
}
// this won't work:
int add(int x, y) {
    return x+y;
}
Unlike e.g. in Go, there is no shorthand syntax for multiple parameters with the same data type. We have to cut them out.

Function overloading

int add(int x) {
    cout << "int function";
    return x+x;
}

double add(double x) {
    cout << "double function";
    return x+x;
}

int main(int argc, const char * argv[]) {
    cout << add(2.5); // double function
    cout << add(2);   // int function
}
We can create two or more functions with the same name - as long as they have different datatypes, only the function with the matching datatype will be executed. s

Passing Functions as arguments

int add (int a, int b) {
  return a + b; 
}

int useFn(int a, int b, function<int(int, int)> fn) {
  return fn(a, b); 
}

int main() {
  cout << useFn(2, 3, &add) << endl; 
}

Passing Functions as arguments

void PrintValue(int value) {
  cout << "Value: " << value << endl;
}
    
void ForEach(
  const vector<int> &values, void (*func)(int)
) {
  for (int value : values) {
    func(value);
  }
}
    
int main() {
  vector<int> values = {1, 2, 3, 4, 5};
  ForEach(values, PrintValue);
  return 0;
}

Lambda Functions

auto lambda = []() { cout << "Hi" << endl; };

lambda(); 
int a = 5; 

auto lambda = [a]() { cout << "Hi" << endl; };

lambda();
Lambdas are throw-away functions, and do not lead to a physical presence of a function. The "=" specifies we want to make all objects available inside the function.

#Arrays

Basic array

int numbers[] = {1, 2, 3, 4, 5};
// alternatively: 
int numbers[2]; 
numbers[0] = 1; 
numbers[1] = 2;
For the latter syntax, the size of the array needs to be specified.

For-looping an array

int numberes[5]; 

for (int i = 0; i < 5; i++) 
  numbers[i] = 2; 
This is a shorthand, leaving out the { }

Pointers on arrays

int numbers[5];
int* ptr = numbers;
      
numbers[2] = 5;
// value change: 
*(ptr + 2) = 6;
    
// printing 6   
cout << numbers[2] << endl;

Arrays on the heap

int* anotherArray = new int[5]; 
delete[] anotherArray; 

Usually, arrays are created on the stack - yet, we can put them on the heap, which means the data will last until the program ends or we destroy it manually.

Getting the size of an arary

int a[5]; 
int count = sizeof(a) / sizeof(int); 
Yes, this is the way to do it...

Array as return type

int *returnArr() {
  int coordinates[2] = {1, 2};
  return coordinates;
}
    
int main()  {
  int x = returnArr()[0];
}

Array as parameters

void multiply(int nums[3]) {
  for (int i = 0; i < 3; i++) {
    cout << nums[i] * 2 << endl;
  }
}
    
int main() {
  int nums[3] = {1, 2, 3};
  multiply(nums);
  return 0;
}

#Pointers & References

A pointer

int number = 420; 
int numberPointer = *number;
You can only return one value.