-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz08.tex
562 lines (421 loc) · 9.71 KB
/
quiz08.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
554
555
556
557
558
559
560
561
562
\documentclass[landscape]{slides}
\usepackage[margin=.5in]{geometry}
\usepackage{tabularx}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}
\usepackage{graphics}
\usepackage[english]{babel}
\usepackage{underscore}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\lstset{basicstyle={\tiny\ttfamily},backgroundcolor=\color{hellgelb},frame=single,numbers=left,numberstyle={\tiny\sffamily},firstnumber=auto}
\newcommand{\st}{Slide Title}
\onlyslides{0-999}
\begin{document}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string s("kulturuke");
std::cout << s << std::endl;
while( next_permutation(s.begin(),s.end()) )
std::cout << s << std::endl;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
kulturuke
kultuuekr
kultuuerk
... 21721 more
uuutrlekk
uuutrlkek
uuutrlkke
\end{verbatim}
What if you sort the string before entering the do loop? I get
30240 lines:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
ekklrtuuu
ekklrutuu
ekklruutu
...
uuutrlekk
uuutrlkek
uuutrlkke
\end{verbatim}
Is string a regular container?
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <string>
int main() {
std::string s("Hello World!");
std::wstring ws(s);
std::cout << ws << std::endl;
}
\end{lstlisting}
This code does not compile. How to fix?
\begin{note}
\st
\begin{tiny}
I get errors on both line 6 and 7.
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:6: error: no matching function for call to
'std::basic_string<wchar_t, std::char_traits<wchar_t>,
std::allocator<wchar_t> >::basic_string(std::string&)'
...
scratch.cpp:7: error: no match for 'operator<<' in 'std::cout << ws'
...
\end{verbatim}
Here is one way to "fix":
\begin{verbatim}
#include <iostream>
#include <string>
int main() {
std::string s("Hello World!");
std::wstring ws(s.begin(), s.end());
std::wcout << ws << std::endl;
}
\end{verbatim}
[p586],[p609]
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <string>
int main() {
std::string s1 = "Foo";
std::string s2 = "Gaz";
s2 = s1;
s2[0] = 'B';
std::cout << s1 << std::endl;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
Foo
\end{verbatim}
Just for curiosity, what if you try?
\begin{verbatim}
#include <iostream>
#include <string>
int main() {
char s1[] = "Foo";
char s2[] = "Gaz";
s2 = s1;
s2[0] = 'B';
std::cout << s1 << std::endl;
}
\end{verbatim}
Then I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:7: error: ISO C++ forbids assignment of arrays
scratch.cpp:7: confused by earlier errors, bailing out
\end{verbatim}
(same is true for C99, C89)
And of course, if you attempt 'char *' then you get a runtime error.
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <string>
int main() {
std::string s1 = "abbcccde";
std::string::size_type p = s1.rfind("cc");
s1.replace(p, 2, "XXX");
std::string s2 = s1.substr(3, -2);
std::cout << s2 << std::endl;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:8: warning: passing negative value '-0x00000000000000002'
for argument 2 to 'std::basic_string<_CharT, _Traits, _Alloc>
std::basic_string<_CharT, _Traits, _Alloc>::substr(typename _Alloc::size_type,
typename _Alloc::size_type) const [with _CharT = char, _Traits
= std::char_traits<char>, _Alloc = std::allocator<char>]'
cXXXde
\end{verbatim}
\end{tiny}
What would the value of 'p' be if the substring "cc" was not found? std::string::npos which is 4294967295 on my machine.
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
int main() {
int a = 4;
int b = 2;
std::clog << a << b;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
42
\end{verbatim}
What is the difference between cerr and clog?
Why was '<<' and '>>' chosen?
[p607] '<<' and '>>' was chosen also because they bind the right way, that is (cout << a) << b rather than cout << (a << b);
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
struct A {
virtual std::ostream & put(std::ostream &) const = 0;
};
struct B : A {
std::ostream & put(std::ostream & s) const { return s << 'B'; }
};
std::ostream & operator<<(std::ostream & s, const A & a) {
return a.put(s);
}
int main() {
B b;
std::cout << b << std::endl;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ scratch.cpp && ./a.out
B
\end{verbatim}
[p612] This is a fine way to print out objects for which only a base class is known.
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
int main() {
double pi = 3.14159265358979323846;
std::cout << pi << std::endl;
std::cout.precision(3);
std::cout << pi << std::endl;
}
\end{lstlisting}
What does this print out? How could this code might look
like if we were using a stream manipulator instead?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ scratch.cpp && ./a.out
3.14159
3.14
\end{verbatim}
You might consider using a manipulator instead. Eg,
\begin{verbatim}
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.14159265358979323846;
std::cout << pi << std::endl;
std::cout << std::setprecision(3) << pi << std::endl;
}
\end{verbatim}
[21.4.6.2 Standard I/O Manipulators, p633]
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::string ostr = "This is a test of writing and reading from files";
std::ofstream ofile("myfile.tmp");
ofile << ostr;
std::string istr;
std::ifstream ifile("myfile.tmp");
ifile >> istr;
std::cout << istr;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get nothing, beacuse ofile is not flushed before we start reading
from the file. Adding a ofile.flush() might help a bit. You can
also do an explisit ofile.close(), but this is also implicitly
done by the destructor [p639] so putting a scoping block around
line 7-9 will work.
If you do that, then you will get 'This' printed out. Why? Because
the >> will read tokens delimited by whitespace. If you want to
read a whole line you might consider:
\begin{verbatim}
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::string ostr = "This is a test of writing and reading from files";
std::ofstream ofile("myfile.tmp");
ofile << ostr;
ofile.close();
std::string istr;
std::ifstream ifile("myfile.tmp");
std::getline(ifile,istr);
std::cout << istr;
}
\end{verbatim}
which might give:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
This is a test of writing and reading from files
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <limits>
int main() {
std::cout << std::numeric_limits<char>::digits << std::endl;
std::cout << std::numeric_limits<int>::digits << std::endl;
std::cout << std::numeric_limits<int>::max() << std::endl;
std::cout << std::numeric_limits<int>::min() << std::endl;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
7
31
2147483647
-2147483648
\end{verbatim}
or:
\begin{verbatim}
g++ -funsigned-char -Wall scratch.cpp && ./a.out
8
31
2147483647
-2147483648
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <valarray>
double f(double d) {
return d + 1;
}
int main() {
const double a[] = {1.23, -4.54, 0.48, -1};
const double b[] = {1, 0, 0, 1};
std::valarray<double> va(a,4);
std::valarray<double> vb(b,4);
std::valarray<double> vc = va * vb;
vc *= 2;
std::valarray<double> vd = vc.apply(f);
for (size_t i=-0; i<vd.size(); i++)
std::cout << vd[i] << " ";
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
3.46 1 1 -1
\end{verbatim}
\end{tiny}
[p662] valarray - designed specifically for speed of the usual numeric vector operations.
\end{note}
\end{slide}
%%%%%
%%
%% \renewcommand\st{§x.x, title}
%% \begin{slide}
%%
%% \begin{lstlisting}
%%
%% \end{lstlisting}
%%
%% what might happen if you try to compile, link and run this program?
%%
%% \begin{note}
%% \st
%%
%% \begin{tiny}
%% \begin{verbatim}
%%
%% \end{verbatim}
%% \end{tiny}
%%
%% \end{note}
%%
%% \end{slide}
\end{document}