diff --git a/20240627/ad-20240627.pdf b/20240627/ad-20240627.pdf index 624d5aa3b3f3d195b259753daef63096362fc819..37dce56d5afc712f44f2c94f88f5e7122446f601 100644 Binary files a/20240627/ad-20240627.pdf and b/20240627/ad-20240627.pdf differ diff --git a/20240627/ad-20240627.tex b/20240627/ad-20240627.tex index e742ec87da1438f0a6211e299790c22864788d90..bfa200d3e9373572a649977288760716b14f1a23 100644 --- a/20240627/ad-20240627.tex +++ b/20240627/ad-20240627.tex @@ -492,6 +492,8 @@ \end{itemize} \end{frame} +\iffalse + \subsection{Templates} \begin{frame}[fragile] @@ -778,6 +780,8 @@ \end{itemize} \end{frame} +\fi + \section{Hardwarenahe Algorithmen} \begin{frame} diff --git a/20240711/Zeichen_123.pdf b/20240711/Zeichen_123.pdf new file mode 120000 index 0000000000000000000000000000000000000000..fdbc897227df059cfda790a16555e6e417682116 --- /dev/null +++ b/20240711/Zeichen_123.pdf @@ -0,0 +1 @@ +../common/Zeichen_123.pdf \ No newline at end of file diff --git a/20240711/ad-20240711.pdf b/20240711/ad-20240711.pdf new file mode 100644 index 0000000000000000000000000000000000000000..62056f01cad5b8fad7345b1b9a5596d27da26ea0 Binary files /dev/null and b/20240711/ad-20240711.pdf differ diff --git a/20240711/ad-20240711.tex b/20240711/ad-20240711.tex new file mode 100644 index 0000000000000000000000000000000000000000..b166d26123924b26d6a3599aca772b81962bf89a --- /dev/null +++ b/20240711/ad-20240711.tex @@ -0,0 +1,444 @@ +% ad-20240711.pdf - Lecture Slides on Algorithms and Data Structures in C/C++ +% Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023, 2024 Peter Gerwinski +% +% This document is free software: you can redistribute it and/or +% modify it either under the terms of the Creative Commons +% Attribution-ShareAlike 3.0 License, or under the terms of the +% GNU General Public License as published by the Free Software +% Foundation, either version 3 of the License, or (at your option) +% any later version. +% +% This document is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this document. If not, see <http://www.gnu.org/licenses/>. +% +% You should have received a copy of the Creative Commons +% Attribution-ShareAlike 3.0 Unported License along with this +% document. If not, see <http://creativecommons.org/licenses/>. + +% README: C++: Templates, Container, Iteratoren, Exceptions, Typ-Konversionen, Intelligente Zeiger + +\documentclass[10pt,t]{beamer} + +\usepackage{pgslides} +\usepackage{tikz} +%\usepackage{rotating} + +\newcommand{\underconstruction}{% + \begin{picture}(0,0) + \color{black} + \put(6,-2.2){\makebox(0,0)[b]{\includegraphics[width=1.5cm]{Zeichen_123.pdf}}} + \put(6,-2.5){\makebox(0,0)[t]{\shortstack{Änderungen\\vorbehalten}}} + \end{picture}} + +\title{Algorithmen und Datenstrukturen in C/C++} +\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski} +\date{11.\ Juli 2024} + +\begin{document} + +\maketitleframe + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \underconstruction + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/ad}}} +% \item[\textbf{\color{red}i}] {\color{red}Online-Werkzeuge für Home Office, Lehre\\ +% und Kommunikation mit Unterdrückten} + \item[\textbf{2}] \textbf{Arrays und Zeiger für Fortgeschrittene} + \item[\textbf{3}] \textbf{Langzahl-Arithmetik} + \item[\textbf{4}] \textbf{Kryptographie} + \item[\textbf{5}] \textbf{Datenkompression} + \item[\color{orange}\textbf{6}] \textbf{\color{orange}Einführung in C++, Datenorganisation} + \item[\color{orange}\textbf{7}] \textbf{\color{orange}Hardwarenahe Algorithmen} + \end{itemize} + +\end{frame} + +\setcounter{section}{5} +\section{Einführung in C++, Datenorganisation} +\setcounter{subsection}{5} +\subsection{Objekte} + +\begin{frame}[fragile] + \showsubsection + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡struct TBase + { + };¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{6cm} + \begin{lstlisting}[gobble=8] + ¡struct TInteger: public TBase + { + int content; + };¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡struct TString: public TBase + { + char *content; + };¿ + \end{lstlisting} + \end{minipage} + \end{center} +\end{frame} + +\addtocounter{subsection}{-1} +\subsection{Objekte: Zugriffsrechte} + +\begin{frame}[fragile] + \showsubsection + \begin{itemize} + \item + \lstinline{public}, \lstinline{private}, \lstinline{protected}\\[\smallskipamount] + nicht nur Bürokratie, sondern auch Kapselung\\ + (Maßnahme gegen "`Namensraumverschmutzung"') + \medskip + \item + \lstinline{struct}: standardmäßig \lstinline{public}\\[\smallskipamount] + \lstinline{class}: standardmäßig \lstinline{private} + \medskip + \item + \lstinline{friend}-Funktionen und -Klassen + \medskip + \item + Klasse als Namensraum:\\ + \lstinline{static}-"`Member"'-Variable\\ + \lstinline{static}-"`Methoden"'\\ + Deklarationen von z.\,B.\ Konstanten und Typen + \end{itemize} +\end{frame} + +\addtocounter{subsection}{-1} +\subsection{Objekte: Konstruktoren und Destruktoren} + +\begin{frame}[fragile] + \showsubsection + \begin{itemize} + \item + leerer Standard-Konstrutor + \item + \newterm{Copy-Konstruktor} + \item + Konstruktor-Aufruf als "`Initialisierung"' + \item + Konstruktor-Aufruf mit \lstinline{new}\\ + Destruktor-Aufruf mit \lstinline{delete} + \item + automatischer Destruktor-Aufruf\\ + beim Verlassen des Gültigkeitsbereichs + \end{itemize} +\end{frame} + +\subsection{Strings} + +\begin{frame}[fragile] + \showsubsection + \begin{itemize} + \item + \lstinline{#include <string>} + \item + String-Klasse + \item + String-Konstante sind \lstinline{const char *} + \item + C-kompatiblen String extrahieren: \lstinline{c_str ()} + \item + In String schreiben: \lstinline{#include <sstream>}, \lstinline{ostringstream} + \end{itemize} +\end{frame} + +\subsection{Templates} + +\begin{frame}[fragile] + \showsubsection + Anwendung desselben Quelltextes auf verschiedene Datentypen + \begin{itemize} + \item + \lstinline{template <typename x> ...} + \item + \lstinline{template <class x> ...} + \end{itemize} +% \pause + Vorsicht: Fehler werden erst bei Instantiierung erkannt! +% \pause + \begin{itemize} + \item + Template-Spezialisierung:\\ + \lstinline{template <> foo <int> ...} + \end{itemize} +\end{frame} + +\subsection{Container-Templates} + +\begin{frame} +% \showsection + \showsubsection + \vspace*{-0.25cm} + \begin{tabular}{ll} + \lstinline|array| & Array mit fester Größe \\ + \lstinline|bitset| & festes Array von Bits (Booleans) \\ + \lstinline|vector| & dynamisches Array \\ + \lstinline|vector <bool>| & dynamisches Bit-Array \\ + \lstinline|forward_list| & einfach-verkettete Liste \\ + \lstinline|list| & doppelt-verkettete Liste \\ + \lstinline|set| & binärer Baum \\ + \lstinline|multiset| & mehrfache Elemente zulässig \\ + \lstinline|unordered_set| & Hash-Tabelle \\ + \lstinline|unordered_multiset| & mehrfache Elemente zulässig \\ + \lstinline|map| & binärer Baum mit separaten Schlüsselwerten \\ + \lstinline|multimap| & mehrere Elemente pro Schlüssel \\ + \lstinline|unordered_map| & Hash-Tabelle mit separaten Schlüsselwerten \\ + \lstinline|unordered_multimap| & mehrere Elemente pro Schlüssel \\ + \lstinline|stack| & Stack \\ + \lstinline|queue| & FIFO \\ + \lstinline|deque| & \emph{\textbf{d}ouble-\textbf{e}nded \textbf{que}ue} \\ + \lstinline|priority_queue| & geordneter Push-Pop-Container + \end{tabular} + \vspace*{-1cm} +\end{frame} + +\subsection{Iteratoren} + +\begin{frame}[fragile] +% \showsection + \showsubsection + + Pointer-Arithmetik: + \medskip + \begin{lstlisting} + int prime[5] = { 2, 3, 5, 7, 11 }; + for (int *p = prime; p != prime + 5; p++) + std::cout << *p << std::endl; + \end{lstlisting} + + \bigskip + Iterator als Verallgemeinerung: + \medskip + \begin{lstlisting} + std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } }; + for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + \end{lstlisting} +\end{frame} + +\subsection{Exceptions} + +\begin{frame}[fragile] + \showsubsection + \begin{lstlisting} + try + { + ... + throw <value>; + ... + } + catch (<type> <variable>) + { + ... + } + catch ... + \end{lstlisting} + \vspace*{-4.6cm}\hspace*{5cm} + \begin{minipage}{7cm} + \begin{itemize} + \item + Nach den \lstinline{catch()}-Statements wird, + soweit nicht anders programmiert, das Programm fortgesetzt. + \medskip + \item + \lstinline{throw;} (ohne Wert):\\ + an übergeordneten Exception-Handler weiterreichen + \medskip + \item + C-Äquivalent:\\ + \lstinline{setjmp()}, \lstinline{longjmp()} + \medskip + \item + speziell für \lstinline{<type>}:\\ + Nachfahren von \lstinline{class exception} + \medskip + \item + veraltet:\\ + \newterm{dynamic exception specifications} + \end{itemize} + \end{minipage} +\end{frame} + +\subsection{Typ-Konversionen} + +\begin{frame}[fragile] + \showsubsection + + \begin{itemize} + \item + In C: + \begin{lstlisting}[gobble=8] + char *hello = "Hello, world!"; + uint64_t address = (uint64_t) hello; + printf ("%" PRIu64 "\n", address); + \end{lstlisting} + \smallskip + \item + alternative Syntax in C++: + \begin{lstlisting}[gobble=8] + char *hello = "Hello, world!"; + uint64_t address = uint64_t (hello); + cout << address << endl; + \end{lstlisting} + \smallskip + \item + zusätzlich in C++:\\ + implizite und explizite Typumwandlung zwischen Zeigern auf Klassen\\ + \lstinline{dynamic_cast<>()}\\ + \lstinline{static_cast<>()}\\ + \lstinline{reinterpret_cast<>()}\\ + \lstinline{const_cast<>()} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + \url{http://www.cplusplus.com/doc/tutorial/typecasting/} + + \begin{itemize} + \item + Zuweisung: Zeiger auf abgeleitete Klasse an Zeiger auf Basisklasse\\ + \textarrow\ implizite Typumwandlung möglich + \smallskip + \item + Zuweisung: Zeiger auf Basisklasse an Zeiger auf abgeleitete Klasse\\ + \textarrow\ nur explizite Typumwandlung möglich:\\ + \hspace*{0.76cm}\lstinline{dynamic_cast<>()}, \lstinline{static_cast<>()} + \smallskip + \item + implizite Typumwandlungen in der Klasse definieren: + \begin{itemize} + \item + Initialisierung durch Konstruktor + \item + Zuweisungs-Operator + \item + Typumwandlungsoperator + \end{itemize} + \smallskip + \item + implizite Typumwandlungen ausschalten:\\ + Schlüsselwort \lstinline{explicit} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + \url{http://www.cplusplus.com/doc/tutorial/typecasting/} + + \begin{itemize} + \item + \lstinline{dynamic_cast<>()}\\ + Zuweisung: Zeiger auf Basisklasse an Zeiger auf abgeleitete Klasse\\ + explizite Typumwandlung mit Prüfung, ggf.\ Exception + \smallskip + \item + \lstinline{static_cast<>()}\\ + Zuweisung: Zeiger auf Basisklasse an Zeiger auf abgeleitete Klasse\\ + explizite Typumwandlung ohne Prüfung + \smallskip + \item + \lstinline{reinterpret_cast<>()}\\ + Typumwandlung ohne Prüfung zwischen Zeigern untereinander\\ + und zwischen Zeigern und Integer-Typen + \smallskip + \item + \lstinline{const_cast<>()}\\ + "`\lstinline{const}"' ein- bzw.\ ausschalten + \end{itemize} +\end{frame} + +\subsection{Intelligente Zeiger} + +\begin{frame}[fragile] + \showsubsection + + \textbf{Warum?} + \begin{itemize} + \item + bereits freigegebene Zeiger werden u.\,U.\ weiterhin verwendet + \item + Speicherlecks + \item + uninitialisierte Zeiger + \end{itemize} + + \medskip + + \begin{itemize} + \item + \lstinline{shared_ptr} + \item + \lstinline{weak_ptr} + \item + \lstinline{unique_ptr} + \item + \lstinline{move()} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + \textbf{Wie?} + \begin{itemize} + \item + R-Wert-Referenztypen: \lstinline{&&} + \item + \lstinline{move()}-Funktion + \end{itemize} + + \bigskip + + Literatur: + \begin{itemize} + \item + \url{http://thbecker.net/articles/rvalue_references/section_01.html} + \item + \url{http://www.artima.com/cppsource/rvalue.html} + \end{itemize} +\end{frame} + +\section{Hardwarenahe Algorithmen} + +\begin{frame} + \showsection + + \textbf{Aufgabe:} + Schreiben Sie die Sinusfunktion selbst. + + \smallskip + + \begin{itemize} + \item + Wir setzen nur die Grundrechenarten voraus. + \item + möglichst effizient + \end{itemize} +\end{frame} + +\end{document} diff --git a/20240711/logo-hochschule-bochum-cvh-text-v2.pdf b/20240711/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..4aa99b8f81061aca6dcaf43eed2d9efef40555f8 --- /dev/null +++ b/20240711/logo-hochschule-bochum-cvh-text-v2.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum-cvh-text-v2.pdf \ No newline at end of file diff --git a/20240711/logo-hochschule-bochum.pdf b/20240711/logo-hochschule-bochum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..b6b9491e370e499c9276918182cdb82cb311bcd1 --- /dev/null +++ b/20240711/logo-hochschule-bochum.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum.pdf \ No newline at end of file diff --git a/20240711/pgslides.sty b/20240711/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..5be1416f4216f076aa268901f52a15d775e43f64 --- /dev/null +++ b/20240711/pgslides.sty @@ -0,0 +1 @@ +../common/pgslides.sty \ No newline at end of file