Typen und Initialisierung
| Schreibweise | Bedeutung |
|---|---|
int a{5}; | Initialisierung mit {} (kein Datenverlust) |
auto x = 3.5; | Typ ableiten |
const int N = 10; / constexpr int N = 10; | Konstante / Konstante zur Übersetzungszeit |
int& r = a; | Referenz (Alias) |
const T& | lesende Referenz auf großes Objekt |
T* p = nullptr; | (nicht besitzender) Zeiger |
std::unique_ptr<T>, std::shared_ptr<T> | besitzende Zeiger |
Die Standardbibliothek im Überblick
| Header | Inhalt |
|---|---|
<iostream>, <fstream>, <sstream>, <format> | Ein-/Ausgabe, Dateien, Strings, Formatierung |
<string>, <string_view> | Text |
<vector>, <array>, <deque>, <list> | Folgen |
<map>, <unordered_map>, <set>, <unordered_set> | Zuordnungen und Mengen |
<stack>, <queue> | Adapter |
<algorithm>, <numeric>, <ranges> | Algorithmen |
<memory> | Smart Pointer |
<optional>, <variant>, <tuple>, <utility> | Hilfstypen |
<functional> | std::function, Funktoren |
<chrono>, <random> | Zeit, Zufall |
<thread>, <mutex>, <atomic>, <future> | Nebenläufigkeit |
<filesystem> | Dateisystem |
<stdexcept>, <exception> | Ausnahmen |
<cmath>, <cstdint>, <cstdlib> | Mathematik, feste Ganzzahltypen |
Häufige Algorithmen
| Algorithmus | Zweck |
|---|---|
sort, stable_sort, partial_sort | sortieren |
find, find_if, binary_search, lower_bound | suchen |
count, count_if | zählen |
transform | umwandeln |
copy, copy_if, remove_if, unique | kopieren und filtern |
accumulate, reduce, iota | summieren, füllen |
min_element, max_element, minmax_element | Extrema |
reverse, rotate, shuffle | umordnen |
all_of, any_of, none_of | prüfen |
Klasse im Überblick
class Beispiel {
public:
Beispiel() = default;
explicit Beispiel(int wert) : wert_(wert) {}
virtual ~Beispiel() = default;
int wert() const { return wert_; }
void setze(int w) { wert_ = w; }
virtual std::string name() const { return "Beispiel"; }
bool operator==(const Beispiel&) const = default;
private:
int wert_ = 0;
};
class Abgeleitet final : public Beispiel {
public:
std::string name() const override { return "Abgeleitet"; }
};Wichtige Compiler-Optionen
g++ -std=c++20 -Wall -Wextra -Wpedantic -Wconversion -O2 -g -fsanitize=address,undefined datei.cpp -o prog| Option | Wirkung |
|---|---|
-std=c++20 / c++23 | Sprachstandard |
-Wall -Wextra -Wpedantic | Warnungen |
-O0 / -O2 / -O3 | Optimierung |
-g | Debuginformationen |
-fsanitize=... | Laufzeitprüfung |
-c, -o | nur übersetzen, Ausgabedatei |
-I, -L, -l | Header-/Bibliothekspfade, Bibliothek |
C++-Versionen
| Standard | Wichtige Neuerungen |
|---|---|
| C++11 | auto, Lambdas, Smart Pointer, Move, nullptr, Range-for, Threads |
| C++14 | generische Lambdas, make_unique |
| C++17 | Structured Bindings, optional, variant, string_view, filesystem, if constexpr |
| C++20 | Concepts, Ranges, Module, <=>, std::format, span, jthread |
| C++23 | std::expected, std::print, import std |