final version

  • [[inf1_cheatsheet.pdf]]
  • [[inf1_cheatsheet.zip]]

Expressions & Operators

Expressions: repräsentieren Berechnungen, haben Typ und
Wert, entweder primär oder zusammengesetzt (mit Operatoren).
Literale: konstanter Wert, fester Typ. 42u, 3.14f, "bruh"

L-Wert R-Wert
Identifiziert Speicherplatz
(Adresse!)
Jeder L-Wert kann als R-
Wert benutzt werden
Kann Wert ändern (z.B.
Zuweisung)
Kann Wert nicht ändern
e.g. Variable e.g. Literal
y - R-Werte: R*R, !R, L++

(-> means from left, <- from right)

Prec. Operator Description Ass.
1 :: scope resolution ->
2 a++ a--, a(), a[], .-> suffix incr./decr.
function call
subscript
member access
<-
3 ++a --a, +a -a, !, *a, &a, new new[], delete delete[] prefix incr./decr.
unary plus/minus
logical NOT
dereference
address of
alloc
dealloc
->
5 a*b a/b a%b mult, div, rem ->
6 a+b a-b add, subtr ->
8 <=> three-way-comp (?) ->
9 < <= > >= order relations ->
10 == != equality relations ->
14 && logical AND ->
15 || logical OR ->
16 a ? b : c, =, += -= &= /= %= ternary op, direct assignment, plus/minus compound assignment, mult/div/rem comp ass <-
17 , comma ->

Modulo: Es gelten (a)/b=(a/b) und (a/b)b+a%b=a.

std library

std::swap(a,b) // swaps values
std::sqrt(a) // square root
std::abs(a,b) // absolute difference between a,b

Types

char/string

float/double

F(β,p,emin,emax)

-> basis, precision, min/max power

normalisiertes Fliesskommasystem:

±d0.d1dp1,d00

-> 0, sowie Zahlen kleiner als βemin haben keine normalisierte Darstellung

Anzahl Zahlen im positiven normalisierten System:

bbits mantissenexponenten= bp1(emaxemin+1)

-> mit negativen Zahlen verdoppelt sich die Anzahl möglicher Zahlen

Number Representation

Rule

  1. Don't compare rounded floats
  2. Dont add two floats of different magnitudes
  3. Dont subtract two floats of similar magnitudes

base conversion

hex bin dec hex bin dec
0 0000 0 8 1000 8
1 0001 1 9 1001 9
2 0010 2 a 1010 10
3 0011 3 b 1011 11
4 0100 4 c 1100 12
5 0101 5 d 1101 13
6 0110 6 e 1110 14
7 0111 7 f 1111 15
Hex Dec Hex Dec Hex Dec Hex Dec
00 0 40 64 80 128 c0 192
10 16 50 80 90 144 d0 208
20 32 60 96 a0 160 e0 224
30 48 70 112 b0 176 f0 240

Primzahltest (n = Input):

unsigned int d;
for(d = 2; n%d != 0; ++d);
bool is_prime = (n == d);

Dec2bin (x ganze Zahl)

for (p = 1; p <= x / 2; p *= 2); // 2p <= x
	for (; p != 0; p /= 2) {
		if (x >= p) {
			std::cout << "1";
			x -= p; }
		else std::cout << "0"; }
10/2=505/2=212/2=101/2=01in reverse order: 1010=10102

Dec2bin (0 < x < 2; float/double)

for (int b_0; x != 0; x = 2 * (x - b_0)) {
	b_0 = (x >= 1);
	std::cout << b_0; } // d0.d1-dn
1.251d0=10.252=0.50d1=00.52=11d2=101.2510=1.012

variables

Don't change variable values inside an R-expression!
int i = 2;
for (int i = 0; i < 4; ++i) cout << i; // 0123
cout << i; // 2

functions

non-void function without return leads to undefined behaviour

overloading

void print(double d);
void print(const std::string& str);

const

void process(const int data); // Can't modify `data`
class MyClass {
    int value;
    public:
    int getValue() const { return value; } // Doesn't modify `value`
    void setValue(int v) { value = v; }    // Modifies `value`
};

int& dangerous() {
    int x = 42; // Local variable
    return x;}  // Dangling reference

pointers

const

read from right to left
int const p1 const int
int const* p2 const int, pointer
int* const p3 int, const pointer
int const* const p4 const int, const pointer
dangling pointer
int* ptr = new int(10);
delete ptr;
// ptr is now dangling

multiple delete

int* ptr = new int(10);
delete ptr;
delete ptr; // Undefined behavior
Deleting same memory multiple times leads to undefined behaviour

Smart Pointers

std::unique_ptr (not further discussed)

#include <memory>
std::unique_ptr<int> ptr(new int(10));
// transfer ownership
std::unique_ptr<int> ptr2 = std::move(ptr);

std::shared_ptr

std::shared_ptr<int> ptr(new int(10));
// copying
std::shared_ptr<int> ptr2 = ptr;

dynamic data types

new/delete

arrays

std::vector

#include <vector>
std::vector<T> name(length, init_val)
std::vector<T> name = {1,2,3};
// wahlfreier zugriff
T t = v[i];
T t = v.at(i) // returns error if outside bounds
// iteration (requires multiplication + addition per iteration)
for (int i=0; i<3;i++) cout << v[i];
// sqeuential interator
for iterator it = v.begin(); it != v.end(); ++it
std::cout << *it; // prints 000
Accessing elements outside bounds of vector leads to undefined behavior.

std::string

#include <string>
using namespace std;

string str1 = "Hello";
string str2("World");
string str3(5, 'a'); // "aaaaa"
// concatenation
string str4 = str1 + " " + str2; // "Hello World"
str1 += " C++"; // "Hello C++"
// acessing chars
char ch = str1[0]; // 'H'
// length (returns uint/t_size)
int len = str1.length(); // or str1.size()
// substring
string substr = str1.substr(0, 5); // "Hello"
// find
size_t pos = str1.find("C++"); // Position of "C++"
// replace
str1.replace(6, 3, "Cpp"); // "Hello Cpp"
// erase
str1.erase(6, 3); // "Hello "
// insert
str1.insert(6, "Cpp"); // "Hello Cpp"
// to int/double/string
int num = stoi(str1);
double dbl = stod(str1);
string str = to_string(123);
if (str1 == str2) { /* ... */ }
if (str1 < str2) { /* ... */ }
// range-based
for (char& ch : str1) {}
// index-based
for size_type i = 0; i < str.length(); ++i {}
// iterator
for iterator it = str.begin(); it != str.end(); ++it {}

classes

struct Cl {
	int val;
	// constructor
	Cl(int value = 0) : val(value) {};
	// deconstructor
	~Cl();
	// copy constructor
	Cl(const Cl& other);
	// assignment operator
	Cl& operator=(const Cl& other);

	// operator overloading
	Cl operator+(const Cl& other);
	bool operator==(const Cl& other);
	// stream overloading (can be left away inside class, if it doesn't need access to private vars)
	friend std::ostream& operator<<ostream& stream, const Cl& obj;
	friend std::istream& operator>>istream& stream, Cl& obj;
};

// example << overload
std::ostream& operator<<ostream& stream, const Cl& obj {
	stream << obj.val;
	return stream;
};
std::istream& operator>>istream& stream, Cl& obj {}
Rule of three

If a type (that manages dynamic memory) defines one of these structures, it needs to implement all three

  • Deconstructor
  • Copy Constructor
  • Assignment operator

other

dec hex value
0-31 0-1F special chars
32-47 20-2F space, !"#$%&'()*+,-./
48-57 30-39 digits (0-9)
58-64 3A-40 :;>=<?@
65-90 41-5A capital letters (A-Z)
91-96 5B-60 `[]^_``
97-122 61-7A letters (a-z)
123-127 7B-7F {|}~, DEL