|
| 1 | +\input{../../_common/preamble} |
| 2 | + |
| 3 | +\title{C++ fundamentals} |
| 4 | +\subtitle{Part 2} |
| 5 | + |
| 6 | +\begin{document} |
| 7 | + |
| 8 | +\maketitle |
| 9 | + |
| 10 | +\begin{frame}[fragile]{Libraries} |
| 11 | + \begin{itemize} |
| 12 | + \item Standard ANSI C library (e.g.\ \mintinline{cpp}{cmath}) |
| 13 | + \item C++ libraries (e.g.\ \mintinline{cpp}{iostream}) |
| 14 | + \item Standard Template Library (e.g.\ \mintinline{cpp}{vector}) |
| 15 | + \item Third-party libraries |
| 16 | + \end{itemize} |
| 17 | +\end{frame} |
| 18 | + |
| 19 | +\section{I/O streams} |
| 20 | + |
| 21 | +\begin{frame}[fragile]{Standard streams} |
| 22 | + \mintinline{cpp}{iostream} declares the standard streams: |
| 23 | + \begin{description} |
| 24 | + \item[\mintinline{cpp}{cin}] Standard input (keyboard) |
| 25 | + \item[\mintinline{cpp}{cout}] Standard output (screen) |
| 26 | + \item[\mintinline{cpp}{cerr}] Standard error (screen) |
| 27 | + \end{description} |
| 28 | + \begin{itemize} |
| 29 | + \item[$\rightarrow$] \mintinline{cpp}{cin} is an |
| 30 | + \mintinline{cpp}{istream} |
| 31 | + \item[$\rightarrow$] \mintinline{cpp}{cout} and \mintinline{cpp}{cerr} |
| 32 | + are \mintinline{cpp}{ostream}s |
| 33 | + \end{itemize} |
| 34 | +\end{frame} |
| 35 | + |
| 36 | +\begin{frame}[fragile]{File streams} |
| 37 | + \mintinline{cpp}{fstream} declares\ldots |
| 38 | + \begin{itemize} |
| 39 | + \item \mintinline{cpp}{ifstream} to read from files |
| 40 | + \item \mintinline{cpp}{ofstream} to write to files |
| 41 | + \end{itemize} |
| 42 | +\end{frame} |
| 43 | + |
| 44 | +\begin{frame}[fragile]{File input} |
| 45 | + \begin{onlyenv}<1> |
| 46 | + \begin{block}{Steps} |
| 47 | + \begin{enumerate} |
| 48 | + \item Construct an \mintinline{cpp}{istream} |
| 49 | + \item Connect it to a file |
| 50 | + \item Read from it using \mintinline{cpp}{<<} |
| 51 | + \item Disconnect the file |
| 52 | + \end{enumerate} |
| 53 | + \end{block} |
| 54 | + \end{onlyenv} |
| 55 | + \begin{onlyenv}<2> |
| 56 | + \begin{cpp} |
| 57 | + #include <fstream> |
| 58 | + |
| 59 | + ifstream input; |
| 60 | + input.open(filename, mode); |
| 61 | + |
| 62 | + // Alternatively: |
| 63 | + ifstream input(filename, mode); |
| 64 | + |
| 65 | + // ... |
| 66 | + |
| 67 | + input.close() |
| 68 | + \end{cpp} |
| 69 | + \end{onlyenv} |
| 70 | +\end{frame} |
| 71 | + |
| 72 | +\begin{frame}[fragile]{File output} |
| 73 | + \begin{onlyenv}<1> |
| 74 | + \begin{block}{Steps} |
| 75 | + \begin{enumerate} |
| 76 | + \item Construct an \mintinline{cpp}{ostream} |
| 77 | + \item Connect it to a file |
| 78 | + \item Write to it using \mintinline{cpp}{>>} |
| 79 | + \item Disconnect the file |
| 80 | + \end{enumerate} |
| 81 | + \end{block} |
| 82 | + \end{onlyenv} |
| 83 | + \begin{onlyenv}<2> |
| 84 | + \begin{cpp} |
| 85 | + #include <fstream> |
| 86 | + |
| 87 | + ofstream output; |
| 88 | + output.open(filename, mode); |
| 89 | + |
| 90 | + // Alternatively: |
| 91 | + ofstream output(filename, mode); |
| 92 | + |
| 93 | + output.close() |
| 94 | + \end{cpp} |
| 95 | + \end{onlyenv} |
| 96 | +\end{frame} |
| 97 | + |
| 98 | +\section{Strings and vectors} |
| 99 | + |
| 100 | +\begin{frame}[fragile]{Strings} |
| 101 | + \begin{itemize} |
| 102 | + \item C represents strings as arrays of \mintinline{cpp}{char}s |
| 103 | + \item In C++, \mintinline{cpp}{string} models character sequences |
| 104 | + \item It also provides functions such as \mintinline{cpp}{length()} |
| 105 | + \end{itemize} |
| 106 | + \begin{cpp} |
| 107 | + #include <string> |
| 108 | + |
| 109 | + string s("Test"); |
| 110 | + cout << s.length() << endl; |
| 111 | + \end{cpp} |
| 112 | +\end{frame} |
| 113 | + |
| 114 | +\begin{frame}[fragile]{Vectors} |
| 115 | + \begin{itemize} |
| 116 | + \item \mintinline{cpp}{vector} represents dynamically\hyp{}sized vectors |
| 117 | + \item It provides functions to add and remove elements |
| 118 | + \end{itemize} |
| 119 | + \begin{cpp} |
| 120 | + #include <vector> |
| 121 | + |
| 122 | + vector<int> v(10); |
| 123 | + cout << v.length() << endl; |
| 124 | + \end{cpp} |
| 125 | +\end{frame} |
| 126 | + |
| 127 | +\section{Useful third-party libraries} |
| 128 | + |
| 129 | +\begin{frame}{Boost} |
| 130 | + \begin{itemize} |
131 | + \item A large collection of general\hyp{}purpose libraries |
| 132 | + \item Many have been incorporated in the C++ standard over the years |
| 133 | + \end{itemize} |
| 134 | + \begin{flushright} |
| 135 | + \includegraphics[width=0.2\textwidth]{figures/boost} |
| 136 | + \end{flushright} |
| 137 | + \vspace{-2em} |
| 138 | +\end{frame} |
| 139 | + |
| 140 | +\begin{frame}{Libraries for scientific computing} |
| 141 | + \begin{itemize} |
| 142 | + \item ALGLIB |
| 143 | + \item Armadillo |
| 144 | + \item Blaze |
| 145 | + \item Dlib |
| 146 | + \item Eigen |
| 147 | + \item NAG |
| 148 | + \item \ldots |
| 149 | + \end{itemize} |
| 150 | +\end{frame} |
| 151 | + |
| 152 | +\end{document} |
| 153 | + |
0 commit comments