-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz07.tex
639 lines (497 loc) · 12.8 KB
/
quiz07.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
\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{§16.2.3, Library Organization and Containers}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <vector>
template<typename T> void p(T x) { std::cout << x; }
int main() {
int my_data[] = {3,7,8,2,5};
std::vector<int> values(&my_data[0], &my_data[sizeof(my_data)/sizeof(int)]);
for(int i=0; i<values.size(); ++i)
p(values[i]);
}
\end{lstlisting}
What will this code print out? Please critizise the code. What will happen
if we use std::list instead of std::vector? Please provide an alternative
implementation of line 9-10.
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:9: warning: comparison between signed and unsigned integer expressions
37825
\end{verbatim}
On line 8, consider writing sizeof(my_data[0]) instead of sizeof(int)
How to rewrite this code to use short or long long instead of ints?
Replacing std::vector with std::list gives:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:9: warning: comparison between signed and unsigned integer expressions
scratch.cpp:10: error: no match for 'operator[]' in 'values[i]'
\end{verbatim}
Notice that list does not have an implementation of the [] operator. Why?
[p441] To avoid the problems of fat interfaces, operations that cannot be efficiently
implemented for all containers are not included in the set of common operations. For
example, subscripting is provided for std::vector but not for std::list.
How can we fix line 9-10? [p546, §18.5.1] Advice: Prefer algorithms to loops.
You might consider:
\begin{verbatim}
for_each(values.begin(), values.end(), p<int>);
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
struct print {
std::string prefix_;
print( std::string prefix ) : prefix_(prefix) {}
void operator() (int i) {
std::cout << prefix_ << i << std::endl;
}
};
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
std::for_each( v.begin(), v.end(), print("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
i=1
i=2
i=3
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§16, Library Organization and Containers}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <list>
#include <algorithm>
template<typename T> void p(T x) { std::cout << x; }
template<class C> typename C::value_type sum(const C & c) {
typename C::value_type s = 0;
for (typename C::const_iterator i = c.begin(); i != c.end(); ++i )
s += *i;
return s;
}
int main() {
typedef long data_type;
data_type my_data[] = {3,7,8};
typedef std::list<data_type> container;
container values(&my_data[0], &my_data[sizeof(my_data)/sizeof(my_data[0])]);
for_each(values.begin(), values.end(), p<data_type>);
p(sum<container>(values));
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
How many lines do we have to change if we want to work with a vector
of ints instead? How can we make this code print "87318" instead?
Why is the keyword typename on line 8 and 9 needed?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
37818
\end{verbatim}
To work with vector of ints instead you have to change 3 lines: 2, 15,
and line 17.
If you want this code to print 87318, you might consider changing line 19:
\begin{verbatim}
for_each(values.rbegin(), values.rend(), p<data_type>);
\end{verbatim}
Typename on line 8 and 9 is needed because C::value_type is a so called
dependent-name. By default, the compiler assumes that you are refering
to a non-type, but in this case it is a type and you specify that by
adding typename infront of the dependent-name. (see note on p444)
See p857 for an example of what a dependent name is. Eg,
\begin{verbatim}
int y;
template<class T> void g(T & v) {
T::x(y); // function call or variable declaration?
};
\end{verbatim}
In this case, T::x(y) is treated as a function call, but
if you add typename in front, it is treaded as a variable
declaration.
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§17.3.3, Priority Queue}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <queue>
#include <string>
template<typename T> void p(T x) { std::cout << x; }
class Message {
std::string msg_;
int priority_;
public:
Message(std::string msg, int priority) : msg_(msg), priority_(priority) {}
bool operator<(const Message & m) const { return priority_ < m.priority_; }
std::string msg() const { return msg_; }
int priority() const { return priority_; }
};
std::ostream & operator<<(std::ostream & ostm, const Message & m) {
return ostm << m.msg() << '(' << m.priority() << ')';
}
int main() {
std::priority_queue<Message> q;
q.push(Message("Foo",4));
q.push(Message("Bar",2));
q.push(Message("Gaz",3));
q.push(Message("Daz",5));
q.push(Message("Boo",8));
while ( !q.empty() ) {
p(q.top());
q.pop();
};
}
\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
Boo(8)Daz(5)Foo(4)Gaz(3)Bar(2)
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <string>
#include <map>
int main() {
std::map<std::string,int> m;
m["foo"] = 2;
m["foo"] = 4;
m["bar"] = 8;
m["gaz"] = 5;
m["gaz"]++;
for( std::map<std::string,int>::iterator i = m.begin();
i != m.end(); ++i) {
std::cout << (*i).first << " " << (*i).second << std::endl;
}
std::pair<std::string,int> b("bar",1);
std::pair<std::map<std::string,int>::iterator,bool> p = m.insert(b);
std::cout << std::boolalpha << p.second << std::endl;
std::cout << m["bar"] << 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
bar 8
foo 4
gaz 6
false
8
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <bitset>
#include <string>
int main() {
std::bitset<8> b = 0x01;
b << 1;
std::cout << b << std::endl;
b |= std::bitset<8>(std::string("11110000"));
std::cout << b << std::endl;
std::cout << b.count() << 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
00000001
11110001
5
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
int main() {
std::vector<int> v1;
v1.push_back(1);
v1.push_back(4);
v1.push_back(3);
v1.push_back(4);
v1.push_back(9);
std::list<int> v2;
std::copy( v1.begin(), v1.end(), front_inserter(v2));
std::copy( v2.rbegin(), v2.rend(), std::ostream_iterator<int>(std::cout) );
}
\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
14349
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <algorithm>
template<typename T> void p(T x) { std::cout << x; }
class my_array {
public:
typedef short value_type;
typedef value_type * iterator;
typedef value_type & reference;
reference operator [] (ptrdiff_t i) { return v[i]; }
iterator begin() { return v; }
iterator end() { return v+size(); }
size_t size() const { return sizeof(v) / sizeof(value_type); }
my_array() : v() {}
private:
value_type v[4];
};
int main() {
my_array m;
m[2] = 4;
m[3] = 2;
std::for_each( m.begin() + 2, m.end(), p<my_array::value_type> );
}
\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
42
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
class Foo {
int v;
public:
Foo(int i) : v(i) {}
void print() { std::cout << v; }
};
int main() {
std::vector<Foo> v;
v.push_back(Foo(1));
v.push_back(Foo(2));
v.push_back(Foo(3));
std::for_each( v.begin(), v.end(), Foo::print );
}
\end{lstlisting}
This code does not compile. How to fix it so that it prints 123?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:18: error: invalid use of non-static member function 'void Foo::print()'
scratch.cpp:18: error: invalid use of non-static member function
/usr/include/c++/4.0.0/bits/stl_algo.h: In function '_Function std::for_each(_InputIterator, \
_InputIterator, _Function) [with _InputIterator = __gnu_cxx::__normal_iterator<Foo*, \
std::vector<Foo, std::allocator<Foo> > >, _Function = void (Foo::)()]':
scratch.cpp:18: instantiated from here
/usr/include/c++/4.0.0/bits/stl_algo.h:158: error: cannot convert 'Foo' to 'Foo*' in \
argument passing
/usr/include/c++/4.0.0/bits/stl_algo.h:159: error: invalid use of non-static member function
\end{verbatim}
How to fix this? Eg, use an adapter:
\begin{verbatim}
std::for_each( v.begin(), v.end(), std::mem_fun_ref(&Foo::print) );
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{tiny}
\begin{lstlisting}
#include <iostream>
#include <algorithm>
#include <vector>
bool equal_to_4_func(int i) {
return i == 4;
}
struct equal_to_4_class {
bool operator()(int i) { return i == 4; }
};
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(4);
v.push_back(9);
std::cout << std::count_if( v.begin(), v.end(), equal_to_4_func );
std::cout << std::count_if( v.begin(), v.end(), equal_to_4_class() );
}
\end{lstlisting}
\end{tiny}
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
22
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
template <int v> bool equal_to_func_templ(int i) {
return i == v;
}
template <int v> struct equal_to_class_templ {
bool operator()(int i) const { return i == v; }
};
struct my_equal_to : public std::binary_function<int, int, bool> {
bool operator() (int a, int b) const { return a == b; }
};
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(4);
v.push_back(9);
using namespace std;
cout << count_if( v.begin(), v.end(), equal_to_func_templ<4> ) << endl;
cout << count_if( v.begin(), v.end(), equal_to_class_templ<4>() ) << endl;
cout << count_if( v.begin(), v.end(), bind2nd(equal_to<int>(),4) ) << endl;
cout << count_if( v.begin(), v.end(), bind1st(my_equal_to(),4) ) << 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
2222
\end{verbatim}
\end{tiny}
\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}