重要通知!!!!!!!!!!!!!
优先队列没有back()操做!!!!!
误人子弟Crloss已经自毙了!!!!!
同时更正了一些小问题,若是想直接知道结果,能够直接跳过验证部分到最下方。
——2018.11.03node
优先队列是一种特殊的队列,在学习堆排序的时候就有所了解,点“击”查看。ios
那么优先队列是什么呢?
说白了,就是一种功能强大的队列。若是不太清楚队列,能够看看我这篇博客。web
它的功能强大在哪里呢?
四个字:自动排序。数组
首先,你须要数据结构
#include<queue> using namespace std;//这个不是头文件但我也不晓得叫什么专业定语反正要和头文件一块儿开就是了!
这两个头文件东西。less
using namespace std; 这句话,表明,使用一个叫作“std”的namespace,namespace里面封存了一系列东西,比方说奇异的数据结构和奇异的函数。当打开了namespace之后,就跟打开了头文件的本质是同样的,都是能够直接用它里面封存的函数。
不一样之处在什么地方?就是不开namespace的使用,在你想用的(以std为例)函数面前加上“std::”便可。
例如,std::sort(a+1,a+1+N); 之类的。
感谢 @龙征天 的评论指点!svg
其次,一个优先队列声明的基本格式是:
priority_queue<结构类型> 队列名;
好比:函数
priority_queue <int> i; priority_queue <double> d;
不过,咱们最为经常使用的是这几种:学习
priority_queue <node> q; //node是一个结构体 //结构体里重载了‘<’小于符号 priority_queue <int,vector<int>,greater<int> > q; //不须要#include<vector>头文件 //注意后面两个“>”不要写在一块儿,“>>”是右移运算符 priority_queue <int,vector<int>,less<int> >q;
咱们将在下文来说讲这几种声明方式的不一样。spa
与队列的基本操做一模一样。
若是想要了解请点击这里,看关于队列的介绍。
以一个名为q的优先队列为例。
q.size();//返回q里元素个数 q.empty();//返回q是否为空,空则返回1,不然返回0 q.push(k);//在q的末尾插入k q.pop();//删掉q的第一个元素 q.top();//返回q的第一个元素
上文已经说过了,自动排序。
怎么个排法呢?
在这里介绍一下:
priority_queue <int> q;
这样的优先队列是怎样的?让咱们写程序验证一下。
#include<cstdio> #include<queue> using namespace std; priority_queue <int> q; int main() { q.push(10),q.push(8),q.push(12),q.push(14),q.push(6); while(!q.empty()) printf("%d ",q.top()),q.pop(); }
程序大意就是在这个优先队列里依次插入十、八、十二、1四、6,再输出。
结果是什么呢?
14 12 10 8 6
也就是说,它是按从大到小排序的!
先看看这个结构体是什么。
struct node { int x,y; bool operator < (const node & a) const { return x<a.x; } };
这个node结构体有两个成员,x和y,它的小于规则是x小者小。
再来看看验证程序:
#include<cstdio> #include<queue> using namespace std; struct node { int x,y; bool operator < (const node & a) const { return x<a.x; } }k; priority_queue <node> q; int main() { k.x=10,k.y=100; q.push(k); k.x=12,k.y=60; q.push(k); k.x=14,k.y=40; q.push(k); k.x=6,k.y=80; q.push(k); k.x=8,k.y=20; q.push(k); while(!q.empty()) { node m=q.top(); q.pop(); printf("(%d,%d) ",m.x,m.y); } }
程序大意就是插入(10,100),(12,60),(14,40),(6,20),(8,20)这五个node。
再来看看它的输出:
(14,40) (12,60) (10,100) (8,20) (6,80)
它也是按照重载后的小于规则,从大到小排序的。
↑好好康康这句话!(摘眼镜)
仍是以int为例,先来声明:
priority_queue <int,vector<int>,less<int> > p; priority_queue <int,vector<int>,greater<int> > q;
再次强调:“>
”不要两个拼在一块儿。
话很少说,上程序和结果:
#include<cstdio> #include<queue> using namespace std; priority_queue <int,vector<int>,less<int> > p; priority_queue <int,vector<int>,greater<int> > q; int a[5]={10,12,14,6,8}; int main() { for(int i=0;i<5;i++) p.push(a[i]),q.push(a[i]); printf("less<int>:"); while(!p.empty()) printf("%d ",p.top()),p.pop(); printf("\ngreater<int>:"); while(!q.empty()) printf("%d ",q.top()),q.pop(); }
结果:
less<int>:14 12 10 8 6 greater<int>:6 8 10 12 14
因此,咱们能够知道,less是从大到小,greater是从小到大。
为了装13方便,在平时,建议你们写:
priority_queue<int,vector<int>,less<int> >q; priority_queue<int,vector<int>,greater<int> >q;
平时若是用从大到小不用后面的vector<int>,less<int>
,可能到时候要改为从小到大,你反而会搞忘怎么写greater<int>
,反而得不偿失。
有可能遇到这种状况:心情很差不想用重载小于一个结构体的优先队列,要按照各类不同的规则排序。
固然,若是不是优先队列而是数组,咱们就会多写几个bool函数塞到sort里面来改变它的小于规则,好比:
struct node { int fir,sec; }arr[2030]; bool cmp1(node x,node y) { return x.fir<y.fir; //当一个node x的fir值小于另外一个node y的fir值时,称x<y } bool cmp2(node x,node y) { return x.sec<y.sec; //当一个node x的sec值小于另外一个node y的sec值时,称x<y } bool cmp3(node x,node y) { return x.fir+x.sec<y.fir+y.sec; //当一个node x的fri值和sec值的和小于另外一个node y的fir值和sec值的和时,称x<y } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d %d",&arr[i].fir,&arr[i].sec); puts("\n--------------------"); sort(arr+1,arr+1+n,cmp1); for(int i=1;i<=n;i++) printf("%d. {%d %d}\n",i,arr[i].fir,arr[i].sec); } puts("\n--------------------"); sort(arr+1,arr+1+n,cmp2); for(int i=1;i<=n;i++) printf("%d. {%d %d}\n",i,arr[i].fir,arr[i].sec); } puts("\n--------------------"); sort(arr+1,arr+1+n,cmp3); for(int i=1;i<=n;i++) printf("%d. {%d %d}\n",i,arr[i].fir,arr[i].sec); }
由于不是总体因此就省略了验证环节(也就是说上面那个代码的正确性不保证)
可是优先队列可没有sort那么灵活想用什么做小于规则用什么做小于规则,它只会用一个固定的小于规则。
因此若是想把一个队列按不一样的方式优先,就要:
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n; struct node { int fir,sec; void Read() {scanf("%d %d",&fir,&sec);} }input; struct cmp1 { bool operator () (const node &x,const node &y) const { return x.fir<y.fir; } };//当一个node x的fir值小于另外一个node y的fir值时,称x<y struct cmp2 { bool operator () (const node &x,const node &y) const { return x.sec<y.sec; } };//当一个node x的sec值小于另外一个node y的sec值时,称x<y struct cmp3 { bool operator () (const node &x,const node &y) const { return x.fir+x.sec<y.fir+y.sec; } };//当一个node x的fri值和sec值的和小于另外一个node y的fir值和sec值的和时,称x<y priority_queue<node,vector<node>,cmp1> q1; priority_queue<node,vector<node>,cmp2> q2; priority_queue<node,vector<node>,cmp3> q3; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) input.Read(),q1.push(input),q2.push(input),q3.push(input); printf("\ncmp1:\n"); while(!q1.empty()) printf("(%d,%d) ",q1.top().fir,q1.top().sec),q1.pop(); printf("\n\ncmp2:\n"); while(!q2.empty()) printf("(%d,%d) ",q2.top().fir,q2.top().sec),q2.pop(); printf("\n\ncmp3:\n"); while(!q3.empty()) printf("(%d,%d) ",q3.top().fir,q3.top().sec),q3.pop(); }
读入:
7 1 2 2 1 6 9 9 6 -100 100 -500 20 4000 -3000
输出:
cmp1: (4000,-3000) (9,6) (6,9) (2,1) (1,2) (-100,100) (-500,20) cmp2: (-100,100) (-500,20) (6,9) (9,6) (1,2) (2,1) (4000,-3000) cmp3: (4000,-3000) (6,9) (9,6) (1,2) (2,1) (-100,100) (-500,20)
咱们能够发现啊,priority_queue <int,vector<int>,less<int> > p;
的那个less<int>
其实就表明这个优先队列的小于规则,因此把这个换成cmp1
就会有上述效果,desu~
因此说,因此说啦,必定要记得写全称!
搞定!
优先队列到此就做了个小结。
其实不论是队列,仍是优先队列,都不只仅只有我讲的这些,还有更多能够探索。
应该入门级别都讲得差很少了,以后呢,在下就期待诸君的表演了。
学,无止境。
估摸着本文就讲了几个用法。
一句话,优先队列,是一种能够自动排序的队列。
#include<queue> using namespace std;
要加上这两个头文件。
最完整的声明公式(吧)形如:
priority_queue< 结构名, vector<结构名> , greater/less<结构名> > 队列名;
能够简写为priority_queue<结构名>,不过这样只能从大到小了。
三个结构名请保持一致,如int,double,long long,包括结构体(struct等)。
要求是这个结构要有小于的规则——你要告诉它怎么比较大小,它才能帮你排序。
系统自带的数据结构的小于规则是显然的,对于结构体,须要经过重载运算符等方式规定,如:
struct point { int x,y; bool operator < (const point &p) const { return x*x+y*y<p.x*p.x+p.y*p.y; //假设这是平面上的两个点,规定两个点的大小关系为距离原点的距离小者小。 //这个函数的意思是,当你使用小于运算符判断一个点(假设是a)与另外一个点 //(函数里的p)的大小关系时,系统会判断a.x*a.x+a.y*a.y是否<p.x*p.x+p.y*p.y //若是上述式子成立,就说明a点是小于p点的 ( return 1 ; ->小于运算符得出的结果为真) } }; priority_queue<point> QP;
greater表明升序,即从小到大,
less表明降序,即从大到小,与缩减版无异。
若是不想用重载小于,就新建一个结构体并重载等号,在第三项里填入这个结构体的名字。
q.size();//返回q里元素个数 q.empty();//返回q是否为空,空则返回1,不然返回0 q.push(k);//在q的末尾插入k q.pop();//删掉q的第一个元素 q.top();//返回q的第一个元素
本篇博客讲完了,就这么点儿。