multiset 多重集合容器

与 set 集合容器一样, multiset 多重容器也使用红黑树组织元素数据,只是 multiset 容器允许将重复的元素键值插入,而 set 容器则不允许。multiset 容器实现了 Sorted Associativate Container 、Simple Associative Container 和 Multiple Associative Container 概念的接口规范
    
    在用 multiset 的时候,同样需要引用头文件 "#include <set>"
    说得通俗点,multiset 是比 set 更复杂一点点的容器。下面不重复介绍 multiset 其他的概念和函数,直接来几行代码。关于multiset 的一些其他方面的概念,可以参考 set 容器。
    
    multiset 、map 和 multimap 对 pair , Functor 用得比较多。。。要仔细理解 pair 和 Functor 的用法。(后续文章会讨论 map  multimap )

遍历 multiset 容器元素

/*用前向迭代器将容器中的元素从小到大打印出来
*/-------------------------------------------------------- 遍历 multiset 容器元素
#pragma warning(disable:4786)
#include <set>
#include <iostream>
using namespace std;
int main()
{multiset<int> ms;ms.insert(10);ms.insert(13);ms.insert(11);ms.insert(19);ms.insert(13);ms.insert(19);ms.insert(19);// 打印数据multiset<int>::iterator i, iend;iend = ms.end();for (i=ms.begin(); i!=iend; ++i)cout << *i << ' ';cout << endl;return 0;
}

反向遍历 multiset 容器

 1 /*
 2     使用反向迭代器,将容器中的元素,进行反向遍历,最后打印出来
 3 */
 4
 5 -------------------------------------------------------- 反向遍历 multiset 容器
 6 #include <set>
 7 #include <iostream>
 8 using namespace std;
 9 int main()
10 {
11     multiset<int>  ms;
12     ms.insert(9);
13     ms.insert(5);
14     ms.insert(4);
15     ms.insert(3);
16     ms.insert(7);
17     ms.insert(8);
18     ms.insert(6);
19     ms.insert(10);
20     ms.insert(10);
21     ms.insert(10);
22     ms.insert(4);
23     ms.insert(4);
24     // 反向遍历打印
25     multiset<int>::reverse_iterator ri, riend;
26     riend = ms.rend();
27     for (ri=ms.rbegin(); ri!=riend; ++ri)
28         cout << *ri << ' ';
29     cout << endl;
30
31     return 0;
32 }

multiset 容器的元素搜索

 1 /*
 2     利用 multiset 容器的 find 和 equal_range 函数,搜索键值为 13 的元素
 3 */
 4
 5 -------------------------------------------------------- multiset 容器的元素搜索
 6 #pragma warning(disable:4786)
 7 #include <set>
 8 #include <iostream>
 9 using namespace std;
10 int main()
11 {
12     multiset<int> ms;
13     ms.insert(10);
14     ms.insert(13);
15     ms.insert(12);
16     ms.insert(11);
17     ms.insert(19);
18     ms.insert(13);
19     ms.insert(16);
20     ms.insert(17);
21     ms.insert(13);
22     // 打印所有元素
23     multiset<int>::iterator i, iend;
24     iend  = ms.end();
25     for (i=ms.begin(); i!=iend; ++i)
26         cout << *i << ' ';
27     cout << endl;
28
29     // find 搜索元素 19
30     int v = 19;
31     multiset<int>::iterator i_v = ms.find(v);
32     cout << *i_v << endl;
33
34     // equal_range 搜索元素 13
35     v = 13;
36     pair<multiset<int>::iterator, multiset<int>::iterator>  p = ms.equal_range(v);
37     cout << "大于等于" << v << "的第一个元素 ( X >= k ) 为:" << *p.first << endl;
38     cout << "大于" << v << "的第一个元素( x > k ) 为:" << *p.second << endl;
39
40     // 打印重复键值元素 13
41     multiset<int>::iterator  j;
42     cout << "键值为" << v << "的所有元素为:";
43     for (j=p.first; j!=p.second; ++j)
44         cout << *j << ' ';
45     cout << endl << endl;
46
47     return 0;
48 }

multiset 的其他函数用法

 1 /*
 2     下面的示例程序以学生记录为元素插入 multiset 容器,比较函数是以年龄作比较,利用 size 和 count 函数统计了元素个数和某个键值下的元素个数
 3 */
 4
 5 -------------------------------------------------------- multiset 的其他函数用法
 6 #pragma warning(disable:4786)
 7 #include <set>
 8 #include <iostream>
 9 using namespace std;
10 // 学生结构体
11 struct Student
12 {
13     char* name;
14     int year;
15     char* addr;
16 };
17 // 比较函数
18 struct StudentLess
19 {
20     bool operator()(const Student& s1, const Student& s2) const
21     {
22         return s1.year < s2.year;  // 比较学生年龄
23     }
24 };
25
26 int main()
27 {
28     Student stuArray[] = {
29         {"张三", 23, "北京"},
30         {"李四", 24, "浙江"},
31         {"王五", 25, "上海"},
32         {"何亮", 22, "武汉"},
33         {"何生亮", 23, "深圳"}
34     };
35     // 创建 multiset 对象 ms
36     multiset<Student, StudentLess>  ms(stuArray, stuArray + 5, StudentLess());
37     // 统计
38     cout << "学生人数:" << ms.size() << endl << endl;
39     cout << "年龄为21岁的学生人数:" << ms.count(stuArray[0]) << endl << endl;
40     // 打印元素
41     multiset<Student, StudentLess>::iterator i, iend;
42     iend = ms.end();
43     cout << "姓名    " << "年龄    " << "地址    \n";
44     for (i=ms.begin(); i!=iend; ++i)
45         cout << (*i).name << "    " << (*i).year << "    " << (*i).addr << endl;
46     cout << endl;
47
48     return 0;
49 }
50 /*    从 count 函数输出可看到,真正作为键值的是 Student 结构体中的 year 变量值,而不是一个 Student 元素。因此,虽然 count(stuArray[0]) 传递的是一个 stuArray[0] 元素,但并不是统计等于 stuArray[0] 的元素个数,而是统计等于 stuArray[0].year 的元素个数,即年龄为 21 岁的学生人数为 2。
51
52 */

------------------- multiset 小结
    multiset 多重集合容器是一个可容纳重复元素键值的有序关联容器。与 set 容器一样,使用红黑树作为容器的内部数据结构,元素的搜索操作都是具有对数级的算法时间复杂度。它的 find 和 equal_range 函数,可搜索出某一键值下的所有元素位置。
    
    multiset 缺点:和 set 一样,如果 插入、删除 操作频繁了,multiset 就不适合。
    multiset 优点:相对于 set ,它能插入重复的元素。当然,它的检索速度也是非常快的。

转载于:https://www.cnblogs.com/music-liang/archive/2013/04/06/3001978.html

STL学习笔记-- multiset相关推荐

  1. C++ STL学习笔记

    C++ STL学习笔记一 为何要学习STL: 数据结构与算法是编程的核心,STL中包含各种数据结构和优秀的算法,确实值得深入学习,本文中虽然着重使用,但希望有心的朋友能多看看相关数据结构的实现,对于C ...

  2. C++STL学习笔记(4) 分配器(Allocator)

    在前面的博客<C++ STL学习笔记(3) 分配器Allocator,OOP, GP简单介绍>中,简单的介绍了分配器再STL的容器中所担当的角色,这一节对STL六大部件之一的分配器进行详细 ...

  3. C++ STL学习笔记(3) 分配器Allocator,OOP, GP简单介绍

    继续学习侯捷老师的课程! 在前面的博客<C++ STL学习笔记(2) 容器结构与分类>中介绍了STL中常用到的容器以及他们的使用方法,在我们使用容器的时候,背后需要一个东西支持对内存的使用 ...

  4. C++ STL学习笔记 : 1. template 模板函数

    本篇文章是学习C++ STL库的第一篇笔记,主要记录了使用template关键字创建模板函数的方法. 下面用一个非常简单的例子解释模板函数的用法 : #include <iostream> ...

  5. C++ STL学习笔记(2) 容器结构与分类

    接着学习侯捷老师的C++ STL! 在使用容器的时候,需要明白容器中元素之间在内存里的关系是什么样的,是连续的,还是非连续的. 容器可以分为两类: 1. sequence container , 即序 ...

  6. 【C++ STL学习笔记】C++ STL序列式容器(array,vector,deque,list)

    文章目录 C++ STL容器是什么? 迭代器是什么,C++ STL迭代器(iterator)用法详解 迭代器类别 迭代器的定义方式 C++序列式容器(STL序列式容器)是什么 容器中常见的函数成员 C ...

  7. STL学习笔记(一)

    2019独角兽企业重金招聘Python工程师标准>>> 1. 容器(Containers) 容器分为两类: 序列式容器(Sequence containers),此乃可序群集,其中每 ...

  8. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  9. SGI STL 学习笔记二 vector

    sequence containers Array Vector Heap Priority_queue List sList(not in standard) Deque Stack Queue S ...

最新文章

  1. Chem. Sci. | 3D深度生成模型进行基于结构的从头药物设计
  2. BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏
  3. Java调用JavaScript
  4. 和菜鸟一起学证券投资之股市常见概念公式1
  5. js中同时得到整数商及余数_js和vue实现时分秒倒计时的方法
  6. 深度学习笔记5:正则化与dropout
  7. 从零写一个编译器(三):语法分析之几个基础数据结构
  8. 前端DEMO:网络上流行的抖音罗盘
  9. (64)SPI外设驱动用户发送模块(三)(第13天)
  10. python pandas模块_Python3.5 Pandas模块中Series用法详解
  11. 如何利用MongoDB打造TOP榜小程序
  12. 4-产品需求文档PRD
  13. 区块链应用_资产证券化
  14. TESLA M40折腾笔记
  15. CSDN 2018博客之星评选,感谢大家的投票
  16. Elasticsearch之中文分词器插件es-ik的自定义热更新词库
  17. 2019年北京市社会保险缴费明细表
  18. Java的主要应用领域有哪些?
  19. avformat_open_input返回-1094995529 “Invalid data found when processing input“
  20. 鸿蒙应用开发学习|HarmonyOS工程介绍

热门文章

  1. 带你通俗理解https
  2. 攀枝花a货翡翠,晋城a货翡翠
  3. linux下定时网站文件备份和数据备份以及删除旧备份标准代码
  4. MongoDB集群构建
  5. AppStore刷榜那些事儿:猪,也是这么想的
  6. (转)asp.net夜话之十一:web.config详解
  7. [转]cscope在windows下使用mingw编译的方法
  8. C++11 强类型枚举
  9. C++ 模板实例化与调用
  10. .NET Hacks Tips