跟set集合容器相比,multiset多重集合容器也使用红黑树组织元素,仅仅是multiset多重集合容器同意将反复的元素键值插入。元素的搜索依旧具有对数级的算法时间复杂度,find和equal_range函数能够搜索出某一键值下的全部元素位置。

创建multiset对象

有下面几种方式。

(1)    multiset()

multiset<int> ms;

(2)    multiset(constkey_compare&cmp)

//student结构体

structstudent{

int id;

char *name;

};

//比較函数

structstuLess{

bool oprator()(const student &s1,conststudent &s2){

return s1.id<s2.id;

}

};

//创建multiset对象

multiset<student,stuLess> ms(stuLess);

(3)    multiset(const multiset&)

//multiset<int> ms1;

multiset<int> ms2(ms1);

(4)    miltiset(InputIteratorfirst,InputIterator last)

int array[]={1,2,3,4,5};

multiset<int> ms(array,array+5);

(5)    miltiset(InputIteratorfirst,InputIterator last, const key_compare&cmp)

student stuArray[]={{1,”li”},{2,”shi”},{3,”wang”}};

multiset<student,stuLess> ms(stuArray,stuArray+3,stuLess());

插入

multiset的插入函数与set类似,一般用insert。有三种形式。

(1)    iterator insert(constvalue_type&v)

(2)    iterator insert(iterator pos, constvalue_type&v)

(3)    void insert(InputIteratorfirst,InputIterator last)

删除

multiset的元素删除与set容器的删除全然一致,主要是erase和clear函数。

遍历

利用迭代器进行遍历訪问元素。

#include<iostream>
#include<set>
using namespace std;
int main()
{
multiset<int> ms;
//无序输入
ms.insert(1);
ms.insert(21);
ms.insert(11);
ms.insert(10);
ms.insert(9);
ms.insert(9);
multiset<int>::iterator begin,end;
end=ms.end();
for(begin=ms.begin();begin!=end;begin++)
{
cout<<*begin<<" ";//有序输出
}
cout<<endl;
return 0;
}

反向遍历

利用反向迭代器reverse_iterator和const_reverse_iterator进行反向迭代,降序输出。

#include<iostream>
#include<set>
using namespace std;
int main()
{
multiset<int> ms;
ms.insert(1);
ms.insert(1);
ms.insert(5);
ms.insert(4);
ms.insert(1);
multiset<int>::reverse_iterator rbegin,rend;
rend=ms.rend();
for(rbegin=ms.rbegin();rbegin!=rend;rbegin++)
{
cout<<*rbegin<<" ";
}
cout<<endl;
return 0;
}

元素的搜索

find函数返回第一个搜索到的元素的位置,假设元素不存在,则返回end结束元素位置。

equal_range函数则返回一个能够指示相等元素范围区间的pair对象。原型例如以下:

(1)    iterator find(constkey_type&k)

(2)    pair<iterator,iterator>equal_range(constkey_type&k)

返回一个pair对象。其first变量值为lower_bound(k),second变量值为upper-bound(k),分别指向大于等于k(x>=k)和大于k(x>k)的第一个元素位置。即给出相等元素的位置范围。

#include<iostream>
#include<set>
using namespace std;
int main()
{
multiset<int> ms;
ms.insert(11);
ms.insert(21);
ms.insert(10);
ms.insert(11);
ms.insert(11);
ms.insert(11);
ms.insert(9);
int v=9;
multiset<int>::iterator i_v=ms.find(v);
cout<<*i_v<<endl;
v=11;
pair<multiset<int>::iterator,multiset<int>::iterator>p=ms.equal_range(v);
cout<<"大于等于"<<v<<"的第一个元素为"<<*p.first<<endl;
cout<<"大于"<<v<<"的第一个元素为"<<*p.second<<endl;
//打印反复键值元素11
multiset<int>::iterator i;
cout<<"键值为"<<v<<"的全部元素为";
for(i=p.first;i!=p.second;i++)
{
cout<<*i<<" ";
}
return 0;
}


其它函数

其它经常使用的函数有empty、size、count、lower_bound、upper_bound等。

multiset多重集合容器的更多相关文章

  1. multiset多重集合容器(常用的使用方法总结)

    关于C++STL中multiset集合容器的学习,看别人的代码一百遍,不如自己动手写一遍. multiset多重集合容器和set集合容器的使用方法大多相同,不同的是multiset多重集合容器允许重复 ...

  2. C++STL之multiset多重集合容器

    multiset多重集合容器 multiset与set一样, 也是使用红黑树来组织元素数据的, 唯一不同的是, multiset允许重复的元素键值插入, 而set则不允许. multiset也需要声明 ...

  3. Multiset ------ 多重集合

    Multiset的中文名是多重集合,其实就是集合的扩展版.唯一的不同是集合中一个值只能出现一次,而多重集合中一个值可以出现多次. 粗略看了看MSDN,在STL中,multiset和set的成员函数声明 ...

  4. multiset基础学习,可以有重复类型的多重集合容器

    #include <set> #include <iostream> using namespace std; struct Student { char *name; int ...

  5. C++STL之set集合容器

    set集合容器 set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构, 在 插入元素时, 它会自动调整二叉树的排列, 把该元素放到适当的位置, 以确保每个子树根节点的键 ...

  6. C++ STL set集合容器

    汇总了一些set的常用语句,部分参考了这篇:http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/ #include ...

  7. Codeforces Round #523 (Div. 2) D. TV Shows 模拟(多重集 先把所有区间加入多重集合)+贪心+二分

    题意:给出n个电视节目的起始和结束时间  并且租一台电视需要x +y*(b-a)  [a,b]为时段 问完整看完电视节目的最小花费是多少 思路:贪心的思想 情况1 如果新租一台电视的花费<=在空 ...

  8. python 标准类库-数据类型之集合-容器数据类型

    标准类库-数据类型之集合-容器数据类型   by:授客 QQ:1033553122 Counter对象 例子 >>> from collections import Counter ...

  9. set集合容器(常用的使用方法总结)

     关于C++STL中set集合容器的学习,看别人的代码一百遍,不如自己动手写一遍. 构造set集合容器的目的是为了去重+排序+快速搜索.由于set集合容器实现了红黑树多的平衡二叉检索树的数据结构,在插 ...

随机推荐

  1. Visual Studio写Cuda代码

    1. 正常新建一个项目   2. 在项目中右键, build 选项中选择 CUDA 编译器   3. 项目属性中设置 CUDA 链接库 和 头文件 编译参数等   4. 完成     cu cuh 文 ...

  2. C - Valera and Fruits

    Problem description Valera loves his garden, where n fruit trees grow. This year he will enjoy a gre ...

  3. Hadoop系列之实验环境搭建

    实验环境基本配置 硬件:硬盘单节点50GB,1G内存,单核. 操作系统:CentOS6.4 64bit Hadoop:2.20 64bit(已编译) JDK:jdk1.7 磁盘分区: / 5GB /b ...

  4. jqGrid 排序

    jqgrid 排序: 1.前台和后台交互依靠的是index属性,index属性没有设置情况下获取name属性 2.如下状态是经过处理显示的中文,name属性为StatusStr,没有index属性的情 ...

  5. MySQL 5.6 Reference Manual-14.4 InnoDB Configuration

    14.4 InnoDB Configuration 14.4.1 InnoDB Initialization and Startup Configuration 14.4.2 Configuring ...

  6. PCL:PCL可视化显示点云

    (1):引用:仅仅是简单的显示点云,可以使用CloudViewer类.这个类非常简单易用.但要注意,它不是线程安全的.如果要用于多线程,还要参考PCLVisualizer. 需要注意的是,PointC ...

  7. 杭电 2095 find your present (2)【位运算 异或】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结 ...

  8. 利用chrome浏览器断电调试确定函数触发的位置

    比如某天遇到这样一个问题,页面有一个按钮,上面绑定了事件可能是多个事件,然后我们点击后出现了bug,我们要如何快速定位到这个事件,如果页面只有一个js或少量的js,我们一个打开查找,也可以接受.但是如 ...

  9. 网站顶部显示预加载进度条preload.js

    网站加载的速度快的话,不会显示进度条加载时候的样式. 支持性主流浏览器都支持,ie浏览器需要9以上9也支持. 使用方法 <script src="http://code.jquery. ...

  10. bzoj 1121: [POI2008]激光发射器SZK 思维_结论

    Description 多边形相邻边垂直,边长为整数,边平行坐标轴.要在多边形的点上放一些激光发射器和接收器.满足下列要求: 1发射器和接收器不能放置在同一点: 2发射器发出激光可以沿壁反射,最终到达 ...