C++primer 11.3.1节练习

练习11.15

mapped_type:vector<int>

key_type:int

value_type:pair<const int, vecor<int>>

练习11.16

 1 int main()
 2 {
 3     map<string, int> m1 = { {"asd",1} };
 4     auto it = m1.begin();
 5     int n;
 6     while (cin >> n)
 7     {
 8         it->second = n;
 9     }
10     for (auto w : m1)
11     {
12         cout << w.first << w.second << endl;
13     }
14     system("pause");
15     return 0;
16 }

练习11.17

 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <algorithm>
 8 #include <iterator>
 9 
10 using namespace std;
11 
12 int main()
13 {
14     multiset<string> c = { "good","good","best","never","let","it","rest" };
15     vector<string> v = { "good","good","best","never","let","it","rest" };
16     copy(v.begin(), v.end(), inserter(c, c.end()));//正确 
17     //copy(v.begin(),v.end(),back_inserter(c)); //错误 multiset没有push_back这个操作,尾插法不适合 
18     //copy(c.begin(),c.end(),inserter(v,v.end()));//正确 
19     //copy(c.begin(),c.end(),back_inserter(v));//正确 
20 
21     for (vector<string>::iterator it = v.begin(); it != v.end(); ++it)
22         cout << *it << " ";
23     cout << endl;
24 
25     for (multiset<string>::iterator it = c.begin(); it != c.end(); ++it)
26         cout << *it << " ";
27     cout << endl;
28     system("pause");
29     return 0;
30 }

练习11.18

map<string, int>::const_iterator

 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <algorithm>
 8 #include <iterator>
 9 
10 using namespace std;
11 
12 int main()
13 {
14     map<string, int> word_count;
15     string str;
16     while (cin >> str)
17     {
18         ++word_count[str];
19     }
20     map<string, int>::const_iterator map_it = word_count.cbegin();
21     while (map_it != word_count.end())
22     {
23         cout << map_it->first << " " << (*map_it).second << endl;
24         ++map_it;
25     }
26     system("pause");
27     return 0;
28 }

练习11.19

using F = bool (const Sales_data &,const Sales_data &);
multiset<Sales_data, F*>::iterator it = bookstore.begin();

 

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值