C++primer 11.2.1节练习

练习11.5

map存储的是键值对的集合;

set则是简单的关键字集合;

练习11.6

set 中的元素是有序且唯一的;

list中的元素则没有这么多要求;

两者相同点在于迭代器都是双向的;

练习11.7

 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 using namespace std;
15 using namespace placeholders;
16 
17 
18 
19 int main()                
20 {
21     string str,str1;
22     map<string, vector<string>> family{ {"joyce",{"haha","hehe"}},{"james",{"heihei", "tom", "jerry"}} };
23 
24     for (auto c : family)
25     {
26         cout << "member is: " << endl;
27         for (auto it = c.second.begin(); it != c.second.end(); ++it)
28             cout << *it << "." << c.first << endl;
29         cout << endl;
30     }
31 
32     while (cin >> str >> str1)            
33     {
34         family[str].push_back(str1);
35     }
36 
37     for (auto c : family)
38     {
39         cout << "member is: " << endl;
40         for (auto it = c.second.begin(); it != c.second.end(); ++it)
41             cout << *it << "." << c.first << endl;
42         cout << endl;
43     }
44     system("pause");
45     return 0;
46 }

 

练习11.8

 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 using namespace std;
12 using namespace placeholders;
13 
14 
15 
16 int main()                
17 {
18     vector<string> str{ "the","but","and","or","an","a","The","But" };
19     for (auto c : str)
20         cout << c << endl;
21     cout << endl;
22     set<string> str1{ "the","but","and","or","an","a","The","But" };
23     for (auto c : str1)
24         cout << c << endl;
25     system("pause");
26     return 0;
27 }

set中的元素按照大写再小写的字典顺序排列好了

posted @ 2017-08-15 20:36  五月份小姐  阅读(169)  评论(0)    收藏  举报