【C++ Primer 第11章】2. 关联容器操作

访问元素

在multimap和multiset中查找元素

第一方法:

 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 #include <utility>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     multimap<string, string> author;
10     author.insert(make_pair("鲁迅", "朝花夕拾"));
11     author.insert(make_pair("鲁迅", "阿Q正传"));
12     author.insert(make_pair("鲁迅", "野草"));
13     author.insert(make_pair("罗贯中", "三国演义"));
14     author.insert(make_pair("罗贯中", "隋唐志传"));
15     author.insert(make_pair("琼瑶", "还珠格格"));
16     author.insert(make_pair("琼瑶", "情深深雨蒙蒙"));
17 
18     string search_find("鲁迅");
19     auto entries = author.count(search_find);
20     auto iter = author.find(search_find);
21 
22     cout << search_find << "的作品集:" << endl;
23     while (entries)
24     {
25         cout << iter->second << endl;
26         ++iter;
27         --entries;
28     }
29     return 0;
30 }

运行结果:

猜你喜欢

转载自www.cnblogs.com/sunbines/p/9049790.html