码迷,mamicode.com
首页 > 编程语言 > 详细

C++primer 11.2.3节练习

时间:2017-08-15 23:03:49      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:second   nal   std   sys   blog   namespace   pre   prime   1.2   

练习11.12

 1 #include<iostream>
 2 #include<string>
 3 #include <iostream>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <list>
 7 #include <functional>
 8 #include <iterator>
 9 #include <map>
10 #include <set>
11 #include <utility>
12 using namespace std;
13 using namespace placeholders;
14 
15 int main()                
16 {
17     vector<pair<string, int>> pa;
18     string str;
19     int num;
20     pair<string, int> anon;
21     while (cin >> str >> num)
22     {
23         anon.first = str;
24         anon.second = num;
25         pa.push_back(anon);
26     }
27     for (auto c : pa)
28     {
29         cout << c.first << " " << c.second << endl;
30     }
31     system("pause");
32     return 0;
33 }

练习11.13

 1 #include<iostream>
 2 #include<string>
 3 #include <iostream>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <list>
 7 #include <functional>
 8 #include <iterator>
 9 #include <map>
10 #include <set>
11 #include <utility>
12 using namespace std;
13 using namespace placeholders;
14 
15 int main()                
16 {
17     vector<pair<string, int>> pa;
18     string str;
19     int num;
20     pair<string, int> anon;
21     while (cin >> str >> num)
22     {
23         //anon.first = str;
24         //anon.second = num;
25         //anon = pair<string, int>(str, num);
26         anon = make_pair(str, num);
27         pa.push_back(anon);
28     }
29     for (auto c : pa)
30     {
31         cout << c.first << " " << c.second << endl;
32     }
33     system("pause");
34     return 0;
35 }

各有各的好处,第一种最简单明了,其他则更加简洁;

练习11.14

 1 #include<iostream>
 2 #include<cctype>
 3 #include<string>
 4 #include <iostream>
 5 #include <string>
 6 #include <vector>
 7 #include <algorithm>
 8 #include <list>
 9 #include <functional>
10 #include <iterator>
11 #include <fstream>
12 #include <map>
13 #include <set>
14 #include <utility>
15 using namespace std;
16 using namespace placeholders;
17 
18 
19 
20 int main()
21 {
22     string str, str1,str2;
23     int num;
24     map<string, vector<string>> family{ { "joyce",{ "haha","hehe" } },{ "james",{ "heihei", "tom", "jerry" } } };
25     vector<pair<string, int>> brith;
26     pair<string, int> br;
27     for (auto c : family)
28     {
29         cout << "member is: " << endl;
30         for (auto it = c.second.begin(); it != c.second.end(); ++it)
31             cout << *it << "." << c.first << endl;
32         cout << endl;
33     }
34 
35     while (cin >> str >> str1 >> num)
36     {
37         family[str].push_back(str1);
38         br = make_pair(str, num);
39         brith.push_back(br);
40     }
41 
42     for (auto c : family)
43     {
44         cout << "member is: " << endl;
45         for (auto it = c.second.begin(); it != c.second.end(); ++it)
46             cout << *it << "." << c.first << endl;
47         cout << endl;
48     }
49 
50     for (auto c : brith)
51     {
52         cout << c.first << " " << c.second << endl;
53     }
54     system("pause");
55     return 0;
56 }

 

C++primer 11.2.3节练习

标签:second   nal   std   sys   blog   namespace   pre   prime   1.2   

原文地址:http://www.cnblogs.com/wuyinfenghappy/p/7367990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!