multiset
来源:互联网 发布:java实现公告管理系统 编辑:程序博客网 时间:2024/04/29 18:27
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 容器元素#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;}
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 }
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 搜索元素 1930 int v = 19;31 multiset<int>::iterator i_v = ms.find(v);32 cout << *i_v << endl;33 34 // equal_range 搜索元素 1335 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 // 打印重复键值元素 1341 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 }
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 Student12 {13 char* name;14 int year;15 char* addr;16 };17 // 比较函数18 struct StudentLess19 {20 bool operator()(const Student& s1, const Student& s2) const21 {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 对象 ms36 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 ,它能插入重复的元素。当然,它的检索速度也是非常快的。
- multiset
- multiset
- multiset
- multiset
- multiset
- multiset
- multiset
- multiset
- multiset
- multiset
- Set/Multiset:
- STL MultiSet
- c++ multiset
- stl multiset
- Set & MultiSet
- hdu2275(multiset)
- multiset操作
- Set & Multiset
- set
- String,StringBuffer与StringBuilder的区别??
- android 数据变化时notifyDataSetChanged 无效的解决方案
- 解决 Android Nougat requires the IDE to be running with Java 1.8 or later Install a supported JDK
- 面试题之连续子数组的最大和问题,矩形覆盖问题
- multiset
- Android中View的动画效果
- bzoj 1090 [SCOI2003]字符串折叠
- map
- 70多G的Kindle电子书合集
- JS中的基本概念易忘点
- maven项目导入eclipse
- JS中超越现实的匿名函数
- multimap