A Brief Introduction to Multiset[STL]
来源:互联网 发布:学生 mysql云数据库 编辑:程序博客网 时间:2024/04/26 07:17
基础
multiset是<set>库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存在重复的数。
我们通过一个程序来看如何使用multiset。
#include <string>#include <iostream>#include <set>using namespace std;int main(){ int x; scanf("%d",&x); multiset<int>h;//初始h为空 while(x!=0){ h.insert(x);//将x插入h中 scanf("%d",&x); } while(!h.empty()){ __typeof(h.begin()) c=h.begin();//c指向h序列中第一个元素的地址,第一个元素是最小的元素 printf("%d ",*c); h.erase(c);//从h序列中将c指向的元素删除 }}
对于输入数据32 61 12 2 12 0,该程序的输出是2 12 12 32 61。
当要在h中插入一个数x时,语法为h.insert(x);
当在h中删除指针c指向的元素*c时,语法为h.erase(c)。
注意,如果我们把h.erase(c)写成h.erase(*c),那么该语句就会把h中所有和*c相等的元素都删掉
如果要查找最大的元素并赋值给k,语法是
int k=*(h.end()--);
end 指向 just beyond the last element
如果要想知道当前序列中比k大的元素最小的是多少,那么可以这样
int p=*(h.upper_bound(k));
其中h.upper_bound(k)表示比 k 大的最小的数的地址。
不光是int类型,multiset还可以存储其他的类型诸如 string类型,结构(struct或class)类型。而我们一般在编程当中遇到的问题经常用到多关键字的类型,即struct或class。
例如下面的例子:
struct rec{
int x,y;
};
multiset<rec>h;
以上的代码是没有任何用处的,因为multiset并不知道如何去比较一个自定义的多关键字类型。
我们可以定义multiset里面rec类型变量之间的小于关系的含义(这里以x为第一关键字为例),具体过程如下:
我们定义一个比较类cmp,cmp内部的operator函数的作用是比较rec类型a和b的大小(以x为第一关键字,y为第二关键字):
struct cmp{
bool operator()(const rec&a,const rec&b){
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
};
此时rec以及multiset的定义部分完整代码可参考如下:
struct rec{ int x,y;};struct cmp{ bool operator()(const rec&a,const rec&b){ return a.x<b.x||a.x==b.x&&a.y<b.y; }};multiset<rec,cmp>h;
通过以上代码,我们就能建立一个集合h使得该集合能够存储和排序多关键字类型
我们来看一个小应用:求从一个点到另一个点的最短路长度,边都是正权。
正权边的最短路问题可以用dijkstra算法来解决,而优化dijkstra算法可以用heap。这里我们来看如何用multiset实现dijkstra+heap。
以下代码省去了输入输出和图的建立。我们光看求最短路的部分。
y代表图中点的编号,而x则代表当前点y与源点的最短距离。
d[0]=0;//源点是0rec a;a.x=0;a.y=0;h.insert(a);while(!h.empty()){//维护已扩展的点距离由小到大 __typeof(h.begin()) c = h.begin(); rec t = (*c); h.erase(c); for(int i=tail[t.y];i;i=next[i]){//前向星存边 int j = p[i]; rec a; if(d[j]==-1){//d[j]==-1表示j还没有被访问 d[j] = t.x+w[i];//扩展 a.x = d[j]; a.y = j; h.insert(a);//加入堆 } else if(d[j] > t.x + w[i]) {//松弛操作(因为需要删除,所以是否访问过要分情况处理) a.x = d[j]; a.y = j;//先构造旧的a, 用来找到a c = h.upper_bound(a); c--; h.erase(c);//删掉旧的a a.x = t.x + w[i]; d[j] = a.x; h.insert(a);//插入新的a } }}
有了multiset类型,我们就不用再去写平衡树一类的东西了,从而大大降低了编程复杂度.
方法
begin() //返回指向第一个元素的迭代器clear() //清除所有元素count() //返回某个值元素的个数empty() //如果集合为空,返回trueend() //返回指向最后一个元素之后的迭代器,不是最后一个元素erase() //删除集合中的元素find() //返回一个指向被查找到元素的迭代器insert() //在集合中插入元素upper_bound() //返回大于某个值元素的迭代器lower_bound() //返回指向大于(或等于)某值的第一个元素的迭代器rbegin() //返回指向集合中最后一个元素的反向迭代器rend() //返回指向集合中第一个元素的反向迭代器size() //集合中元素的数目equal_range() //返回集合中与给定值相等的上下限的两个迭代器get_allocator() //返回集合的分配器key_comp() //返回一个用于比较元素键值的函数value_comp() //返回一个用于比较元素值的函数max_size() //返回集合能容纳的元素的最大限值swap() //交换两个集合变量
关于反向迭代器(随后添加超链接)
集合操作
std::set_intersection();std::set_union() ;std::set_difference();std::set_symmetric_difference();
struct compare{bool operator ()(string s1,string s2){return s1>s2;}///自定义仿函数};
转自百度, 有改动.
- A Brief Introduction to Multiset[STL]
- A Brief Introduction to IoC
- A Brief Introduction to IoC
- A Brief Introduction to REST
- A Brief Introduction to REST
- A Brief Introduction to OVF
- A Brief Introduction to Myself
- A brief introduction to VXLAN
- A Brief Introduction to XACML
- A Brief Introduction to UNIX SHELL Virus
- A brief introduction to geospatial coordinates
- A Brief Introduction to Language Modeling
- A Brief Introduction to PHP Namespacing
- A brief introduction to feature selection
- A Brief Introduction to Digital Video
- IoC 简介:A Brief Introduction to IoC[读后感]
- A Brief Introduction to the JTAG Boundary Scan Interface
- A Brief Introduction to the JCo Server - Austin Sincock
- 怎么查看oracle是否有死锁
- 腾讯2012年实习生笔试加分题 .
- javah用法
- Matlab中的命令hold on & hold off
- liunx增加用户key步骤
- A Brief Introduction to Multiset[STL]
- MDI多种类型文档,多视图
- android加载更多的图片
- 找到字符串中第一个出现一次的字符
- oracle游标讲解
- Win8弊端:XP用户和Vista用户没办法直接升级【mfxp】
- c/c++求平均数
- 生活缴费能力输出
- 数据结构与程序设计练习2.1E4