优先队列 priority_queue 详解

原文链接

优先队列
重要通知!!!!!!!!!!!!!
优先队列没有back()操作!!!!!

引入
优先队列是一种特殊的队列,在学习堆排序的时候就有所了解。

那么优先队列是什么呢?
说白了,就是一种功能强大的队列。

它的功能强大在哪里呢?
四个字:自动排序

优先队列的头文件&&声明

首先,你需要

#include<queue>
using namespace std;//这个不是头文件但我也不晓得叫什么专业定语反正要和头文件一起开就是了!


这两个头文件东西。

using namespace std; 这句话,代表,使用一个叫做“std”的namespace,namespace里面封存了一系列东西,比方说奇异的数据结构和奇异的函数。当打开了namespace以后,就跟打开了头文件的本质是一样的,都是可以直接用它里面封存的函数。
不同之处在什么地方?就是不开namespace的使用,在你想用的(以std为例)函数面前加上“std::”即可。
例如,std::sort(a+1,a+1+N); 之类的。

其次,一个优先队列声明的基本格式是:
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;


我们将在下文来讲讲这几种声明方式的不同。

优先队列的基本操作
与队列的基本操作如出一辙。

以一个名为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();
}


程序大意就是在这个优先队列里依次插入10、8、12、14、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)

它也是按照重载后的小于规则,从大到小排序的。

less和greater优先队列


还是以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是从小到大。

作个总结
在平时,建议大家写:

priority_queue<int,vector<int>,less<int> >q;
priority_queue<int,vector<int>,greater<int> >q;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~
所以说,所以说啦,一定要记得写全称!
搞定!

总结
优先队列到此就作了个小结。
其实不管是队列,还是优先队列,都不仅仅只有我讲的这些,还有更多可以探索。

应该入门级别都讲得差不多了,之后呢,在下就期待诸君的表演了。
学,无止境。

UPD (2018.11.03)
估摸着本文就讲了几个用法。

一句话,优先队列,是一种可以自动排序的队列。

首先是优先队列的各种声明方法。
#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的第一个元素

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值