【C++ Primer Plus】编程练习答案——第18章

 1 // chapter18.h
 2 
 3 #ifndef LEARN_CPP_CHAPTER18_H
 4 #define LEARN_CPP_CHAPTER18_H
 5 
 6 #include 
 7 #include 
 8 
 9 template
10 T average_list(std::initializer_list arr) {
11     T sum = 0;
12     int num = 0;
13     for (auto x = arr.begin(); x != arr.end(); ++ x, ++ num)
14         sum += *x;
15     return sum / num;
16 }
17 void ch18_1();
18 void ch18_2();
19 void ch18_3();
20 void ch18_4();
21 
22 
23 #endif //LEARN_CPP_CHAPTER18_H
24 
25 
26 // chapter18.cpp
27 
28 #include "chapter18.h"
29 
30 void ch18_1() {
31     using namespace std;
32     // list of double deduced from list contents
33     auto q = average_list({15.4, 10.7, 9.0});
34     cout << q << endl;
35     // list of int deduced from list contents
36     cout << average_list({20, 30, 19, 17, 45, 38}) << endl;
37     // forced list of double
38     auto ad = average_list<double>({'A', 70, 65.33});
39     cout << ad << endl;
40 }
41 
42 void ch18_2() {
43 
44 }
45 
46 void ch18_3() {
47 
48 }
49 
50 void ch18_4() {
51 
52 }
53 
54 // 有空再写吧0o0

本文名称:【C++ Primer Plus】编程练习答案——第18章
新闻来源:http://scyanting.com/article/dsogoig.html