Skip to content
Snippets Groups Projects
Commit 0772ad4d authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispiele 21.4.2022

parent 23148b0e
Branches
No related tags found
No related merge requests found
Showing
with 420 additions and 12 deletions
No preview for this file type
...@@ -200,16 +200,16 @@ ...@@ -200,16 +200,16 @@
\begin{lstlisting} \begin{lstlisting}
int prime[5] = { 2, 3, 5, 7, 11 }; int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p != prime + 5; p++) for (int *p = prime; p != prime + 5; p++)
cout << *p << endl; std::cout << *p << std::endl;
\end{lstlisting} \end{lstlisting}
\bigskip \bigskip
Iterator als Verallgemeinerung: Iterator als Verallgemeinerung:
\medskip \medskip
\begin{lstlisting} \begin{lstlisting}
array <int, 5> prime = { { 2, 3, 5, 7, 11 } }; std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
for (array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++) for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++)
cout << *p << endl; std::cout << *p << std::endl;
\end{lstlisting} \end{lstlisting}
\end{frame} \end{frame}
......
No preview for this file type
...@@ -151,22 +151,45 @@ ...@@ -151,22 +151,45 @@
\showsection \showsection
\showsubsection \showsubsection
\begin{onlyenv}<1>
Pointer-Arithmetik: Pointer-Arithmetik:
\medskip \medskip
\begin{lstlisting} \begin{lstlisting}[gobble=6]
int prime[5] = { 2, 3, 5, 7, 11 }; int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p != prime + 5; p++) for (int *p = prime; p != prime + 5; p++)
cout << *p << endl; std::cout << *p << std::endl;
\end{lstlisting} \end{lstlisting}
\bigskip \bigskip
\end{onlyenv}
Iterator als Verallgemeinerung: Iterator als Verallgemeinerung:
\medskip \medskip
\begin{lstlisting} \begin{lstlisting}
array <int, 5> prime = { { 2, 3, 5, 7, 11 } }; std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
for (array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++) for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++)
cout << *p << endl; std::cout << *p << std::endl;
\end{lstlisting}
\bigskip
Mit \lstinline{auto}-Datentyp:
\medskip
\begin{lstlisting}
std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
for (auto p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
\end{lstlisting}
\begin{onlyenv}<2->
\bigskip
Mit Doppelpunkt-Syntax:
\medskip
\begin{lstlisting}[gobble=6]
std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
for (auto p : prime)
std::cout << p << std::endl;
\end{lstlisting} \end{lstlisting}
\end{onlyenv}
\end{frame} \end{frame}
\subsection{Exceptions} \subsection{Exceptions}
......
#include <iostream>
#include <set>
int main ()
{
std::set <int> prime = { { 2, 3, 5, 7, 11 }};
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
bool less (int a, int b)
{
return a > b;
}
int main ()
{
std::set <int, bool (*) (int, int)> prime (less);
// "bool (*less) (int a, int b)"
// wäre eine Variable "less" vom Typ "Zeiger auf eine Funktion,
// die 2 ints erwartet und ein bool zurückgibt".
// Um nur den Typen dieses Zeigers zu bekommen, lassen wir den
// Bezeichner "less" für die Variable weg.
// Nur "bool *less (int a, int b)"
// wäre eine Funktion "less", die einen Zeiger auf bool zurückgibt
// und 2 ints als Parameter erwartet.
prime.insert (2);
prime.insert (3);
prime.insert (7);
prime.insert (11);
prime.insert (13);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
bool less (int a, int b)
{
return a > b;
}
int main ()
{
std::set <int, bool (*) (int, int)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
auto less = [] (int a, int b) { return a > b; };
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [fwd] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [fwd] (int a, int b)
{
fwd = !fwd;
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [&fwd] (int a, int b)
{
fwd = !fwd;
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [fwd] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
fwd = 0;
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [fwd] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
fwd = 0;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [&fwd] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
fwd = 0;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
std::set <int> prime = { { 2, 3, 7, 11, 13, 17, 19 }};
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [const &fwd] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
fwd = 0;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
auto less = [&const fwd] (int a, int b)
{
if (fwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
fwd = 0;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
const bool &cfwd = fwd;
auto less = [&cfwd] (int a, int b)
{
if (cfwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
fwd = 0;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <set>
int main ()
{
bool fwd = 1;
const bool &cfwd = fwd;
auto less = [&cfwd] (int a, int b)
{
cfwd = !cfwd;
if (cfwd)
return a < b;
else
return a > b;
};
std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
fwd = 0;
prime.insert (5);
for (auto p : prime)
std::cout << p << " ";
std::cout << std::endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment