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中的元素按照大写再小写的字典顺序排列好了

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7367611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值