-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture3.tex
553 lines (435 loc) · 14.3 KB
/
lecture3.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
%Lecture 3
%Copyright (C) 2019 Patrick Diehl
%
%This program is free software: you can redistribute it and/or modify
%it 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
\providecommand\classoption{12pt}
\documentclass[\classoption]{beamer}
\input{./template/packages}
\input{template/variables.tex}
% frame slide
\title{\coursename}
\subtitle{Lecture 3: Iterators, Lists, and using library algorithms}
\author{\tiny Patrick Diehl \orcid{0000-0003-3922-8419}}
%\institute {
% \href{}{\tt \scriptsize \today}
%}
\date {
\tiny \url{\courseurl}
\vspace{2cm}
\doclicenseThis
}
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\ifluatex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\begin{document} {
\setbeamertemplate{footline}{}
\frame {
\titlepage
}
}
\frame{
\tableofcontents
}
\AtBeginSection[]{
\begin{frame}
\vfill
\centering
\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
\usebeamerfont{title}\insertsectionhead\par%
\end{beamercolorbox}
\vfill
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Reminder}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Lecture 2}
\begin{block}{What you should know from last lecture}
\begin{itemize}
\item Monte Carlo Methods
\item Random numbers
\item Containers like \lstinline|std::vector|
\item Functions
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Iterators \lstinline|\#include<iterator>|}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Iterators: \lstinline|\#include<iterator>|}
When we know that we access the elements of the vector sequentially, we can let the compiler know that we are doing this by using iterators.
\vspace{1cm}
\begin{block}{Iterators are values that}
\begin{itemize}
\item identifies a container and an element in the container
\item let us access the value stored in that element
\item provides operations for moving between elements
\item are needed for the algorithms of the standard library
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Iterating over vectors}
\begin{block}{Easiest}
\begin{lstlisting}[language=C]
std::vector<int> values;
for(size_t i = 0 ; i < values.size(); i++)
std::cout << values[i] << std::endl;
\end{lstlisting}
\end{block}
\begin{block}{Using the \lstinline|size_type|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/types/size_t}}}
\begin{lstlisting}[language=C]
std::vector<int> values;
std::vector<int>::size_type i = 0;
for(; i < values.size(); i++)
std::cout << values[i] << std::endl;
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Advanced iterating over vectors}
\begin{block}{Example}
\begin{lstlisting}
for(
std::vector<int>::const_iterator iter =
values.begin();
iter != values.end();
++iter
)
{
std::cout << *iter << std::endl;
}
\end{lstlisting}
\end{block}
\begin{block}{Features}
\begin{itemize}
\item \lstinline|const_iterator| allows read-only access
\item \lstinline|++iter| increments the iterator to the next element
\item Dereference the iterator \lstinline|*iter| to access the value
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Erasing elements with iterators gets easier }
\begin{block}{Using the basic way }
\begin{lstlisting}
std::vector<int> values = {1,2,3};
values.erase(values.begin()+i)
\end{lstlisting}
\end{block}
\begin{block}{Using iterators}
\begin{lstlisting}
values.erase(iter)
\end{lstlisting}
\end{block}
Note that with an iterator there is no need to compute the position anymore!
\begin{block}{Useful feature}
\lstinline| iter = values.erase(iter)|
\end{block}
Returns the iterator pointing to the element after the erasure.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Lists \lstinline|\#include<list>|}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Lists vs Vectors}
\begin{block}{Vectors \lstinline|\#include<vector>|}
\begin{itemize}
\item Are sufficient for small amount of elements (around 7000)
\item Is optimized to access elements arbitrary
\item Performs well adding one element by time to its end
\end{itemize}
\end{block}
\begin{block}{Lists \lstinline|\#include<list>| }
\begin{itemize}
\item Are slower for small amount of elements
\item Are optimized to insert and delete elements anywhere
\end{itemize}
\end{block}
\begin{block}{Complexity}
\begin{itemize}
\item Inserting/Removing: Vector $\mathcal{O}(n^2)$ vs List $\mathcal{O}(n)$~\cite{Wirth:1978:ADS:540029,knuth1997art}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Example lists\footnote{\tiny\url{https://en.cppreference.com/w/cpp/container/list}}}
\lstinputlisting{cpp/lecture3-averageList.cpp}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Library algorithms \lstinline|\#include<algorithm>|}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Sorting\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/sort}}}
\begin{block}{Sorting using $<$}
\vspace{-0.25cm}
\begin{lstlisting}
std::sort(s.begin(), s.end());
\end{lstlisting}
\end{block}
\begin{block}{Sorting using $>$\footnote{\tiny\url{https://en.cppreference.com/w/cpp/utility/functional/greater}}}
\vspace{-0.25cm}
\begin{lstlisting}
std::sort(s.begin(), s.end(), std::greater<int>());
\end{lstlisting}
\end{block}
\vspace{-0.25cm}
\begin{block}{Advanced sorting using a lambda expression\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/lambda}} }
\vspace{-0.25cm}
\begin{lstlisting}
std::sort(s.begin(), s.end(), [](int a, int b) {
return a > b; } );
\end{lstlisting}
\end{block}
We will look into lambda expression later in more detail
\end{frame}
\begin{frame}[fragile]{Accumulate}
\begin{block}{Sum\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/accumulate}}}
\vspace{-0.25cm}
\begin{lstlisting}
int sum = std::accumulate(v.begin(), v.end(), 0);
\end{lstlisting}
\end{block}
\vspace{-0.25cm}
\begin{block}{Multiplication\footnote{\tiny\url{https://en.cppreference.com/w/cpp/utility/functional/multiplies}}}
\begin{lstlisting}
int product =
std::accumulate(v.begin(), v.end(), 1,
std::multiplies<int>());
\end{lstlisting}
\end{block}
Note that zero is the initial value of the accumulate
\begin{block}{Various}
\begin{itemize}
\item \lstinline|std::inner_product|
\item \lstinline|std::partial_sum|
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Removing elements}
\begin{block}{Remove\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/remove}}}
\begin{lstlisting}
std::list<int> l = { 1,100,2,3,10,1,11,-1,12 };
l.remove(1); // remove both elements equal to 1
\end{lstlisting}
\end{block}
\vspace{-0.25cm}
\begin{block}{Remove\_if\footnote{\tiny\url{http://www.cplusplus.com/reference/algorithm/remove_if/}}}
\begin{lstlisting}
//Define function
bool IsOdd (int i) {
return ((i%2)==1);
}
//Check for the first odd number
l.remove_if(IsOdd); // remove all odd numbers
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Searching for existence of elements}
\begin{block}{Find\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/find}}}
\begin{lstlisting}
int n1 = 3;
std::vector<int> v{0, 1, 2, 3, 4};
auto result1 =
std::find(std::begin(v), std::end(v), n1);
\end{lstlisting}
\end{block}
\begin{block}{Find\_if\footnote{\tiny\url{http://www.cplusplus.com/reference/algorithm/find_if/}}}
\begin{lstlisting}
//Define function
bool IsOdd (int i) {
return ((i%2)==1);
}
//Check for the first odd number
std::vector<int>::iterator it =
std::find(std::begin(v), std::end(v), IsOdd);
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Search\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/search}}}
Find a substring within a string
\begin{block}{Check for the substring }
\begin{lstlisting}
std::string name = "Math 4997-3";
std::string exp = "4997";
std::search(name.begin(), name.end(),
exp.begin(), exp.end()) != name.end();
\end{lstlisting}
\end{block}
\begin{block}{Get the leading position\footnote{\tiny\url{https://en.cppreference.com/w/cpp/utility/functional/boyer_moore_searcher}}}
\begin{lstlisting}
auto it = std::search(name.begin(), name.end(),
std::boyer_moore_searcher(
exp.begin(), exp.end()));
std::cout << it - name.begin() << std::endl;
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Copy}
\begin{center}
Copy the content of vector a to vector b
\end{center}
\begin{block}{Without library algorithm}
\begin{lstlisting}
typedef std::vector<int>::const_iterator it vit;
for(vit it = a.begin(); it != a.end(); ++it)
{
b.push_back(*it);
}
\end{lstlisting}
\end{block}
\begin{block}{With library algorithm\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/copy}}}
\begin{lstlisting}
std::copy(a.begin(),a.end(),b.begin());
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Insert}
\begin{center}
Append the content of vector a to vector b
\end{center}
\begin{block}{Without library algorithm}
\begin{lstlisting}
typedef std::vector<int>::const_iterator it vit;
for(vit it = a.begin(); it != a.end(); ++it)
{
b.push_back(*it);
}
\end{lstlisting}
\end{block}
\begin{block}{With library algorithm}
\begin{lstlisting}
b.insert(b.end(),a.begin(),a.end());
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Filling vectors}
\begin{block}{Fill vector with one value\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/fill}}}
\begin{lstlisting}
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
//Replace all elements by -1
std::fill(v.begin(), v.end(), -1);
\end{lstlisting}
\end{block}
\begin{block}{Replacing the first 5 elements\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/fill_n}}}
\begin{lstlisting}
//Replace the first 5 elements by -1
std::fill_n(v1.begin(), 5, -1);
\end{lstlisting}
\end{block}
\begin{block}{Appending 5 elements\footnote{\tiny\url{https://en.cppreference.com/w/cpp/iterator/back_inserter}}}
\begin{lstlisting}
//Appending 5 elements
std::fill_n(std::back_inserter(v), 5, -1);
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Transform\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/transform}}}
\begin{block}{Convert to upper case letters}
\begin{lstlisting}
std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) ->
unsigned char { return std::toupper(c); });
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}{Partition}
\lstinputlisting[firstline=3, lastline=20]{cpp/lecture3-partition.cpp}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Numeric limits \lstinline|\#include<limits>|}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Limits}
\lstinputlisting{cpp/lecture3-limits.cpp}
\begin{itemize}
\item \lstinline|::min| returns the smallest finite value of the given type
\item \lstinline|::max| returns the largest finite value of the given type
\end{itemize}
More details about IEEE 754~\cite{goldberg1991every,4610935}
\end{frame}
\begin{frame}{Limits\footnote{\tiny\url{https://en.cppreference.com/w/cpp/types/numeric_limits}}}
\lstinputlisting{cpp/lecture3-limits2.cpp}
\begin{itemize}
\item \lstinline|::round_error| returns the maximum rounding error of the given floating-point type
\item \lstinline|::epsilon| returns the difference between 1.0 and the next representable value of the given type
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{IO}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Writing files\footnote{\tiny\url{https://en.cppreference.com/w/cpp/io/basic_ofstream}}}
\begin{lstlisting}
// basic file operations
#include <iostream>
#include <fstream>
int main () {
std::ofstream myfile;
myfile.open ("example.txt", std::ios::out);
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
\end{lstlisting}
\begin{block}{Mode\footnote{\tiny\url{https://en.cppreference.com/w/cpp/io/ios_base/openmode}}}
\begin{itemize}
\item \lstinline|out| Open for writing (Default)
\item \lstinline|app| Append to the end
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Reading files\footnote{\tiny\url{https://en.cppreference.com/w/cpp/io/basic_ifstream}}~\footnote{\tiny\url{https://en.cppreference.com/w/cpp/io/basic_fstream}}}
\begin{lstlisting}
#include <iostream>
#include <fstream>
#include <string>
int main () {
std::string line;
std::ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::cout << line << '\n';
}
myfile.close();
}
return 0;
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Summary}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Summary}
\begin{block}{After this lecture, you should know}
\begin{itemize}
\item Iterators
\item Lists
\item Library algorithms
\item Numerical limits
\item Reading and Writing files
\end{itemize}
\end{block}
\begin{block}{Further reading:}
C++ Lecture 1 - The Standard Template Library\footnote{\tiny\url{https://www.youtube.com/watch?v=asGZTCR53KY&list=PL7vEgTL3FalY2eBxud1wsfz8OKvE9sd_z}}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{References}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t, allowframebreaks]
\frametitle{References}
\bibliographystyle{plain}
\bibliography{bib}
\end{frame}
\end{document}