priority_queue的用法「建议收藏」

priority_queue的用法「建议收藏」priority_queue本质是一个堆。1.头文件是#include<queue>2.关于priority_queue中元素的比较模板申明带3个参数:priority_queu

大家好,又见面了,我是你们的朋友全栈君。

priority_queue本质是一个堆。

1. 头文件是#include<queue>

2. 关于priority_queue中元素的比较

  模板申明带3个参数:priority_queue<Type, Container, Functional>,其中Type 为数据类型,Container为保存数据的容器,Functional 为元素比较方式。

  Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector。

2.1 比较方式默认用operator<,所以如果把后面2个参数缺省的话,优先队列就是大顶堆(降序),队头元素最大。特别注意pair的比较函数

  以下代码返回一个降序输出:

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 int main(){
 5     priority_queue<int> q;
 6     for( int i= 0; i< 10; ++i ) q.push(i);
 7     while( !q.empty() ){
 8         cout<<q.top()<<endl;
 9         q.pop();
10     }
11     return 0;
12 }

 

  以下代代码返回pair的比较结果,先按照pair的first元素降序,first元素相等时,再按照second元素降序:

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 int main(){
 6     priority_queue<pair<int,int> > coll;
 7     pair<int,int> a(3,4);
 8     pair<int,int> b(3,5);
 9     pair<int,int> c(4,3);
10     coll.push(c);
11     coll.push(b);
12     coll.push(a);
13     while(!coll.empty())
14     {
15         cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
16         coll.pop();
17     }
18     return 0;
19 }

 

 

2.2 如果要用到小顶堆,则一般要把模板的3个参数都带进去。STL里面定义了一个仿函数greater<>,基本类型可以用这个仿函数声明小顶堆

  以下代码返回一个升序输出:

 1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4 int main(){
 5     priority_queue<int, vector<int>, greater<int> > q;
 6     for( int i= 0; i< 10; ++i ) q.push(10-i);
 7     while( !q.empty() ){
 8         cout << q.top() << endl;
 9         q.pop();
10     }
11     return 0;
12 }

 

  以下代代码返回pair的比较结果,先按照pair的first元素升序,first元素相等时,再按照second元素升序: 

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 int main(){
 6     priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > coll;
 7     pair<int,int> a(3,4);
 8     pair<int,int> b(3,5);
 9     pair<int,int> c(4,3);
10     coll.push(c);
11     coll.push(b);
12     coll.push(a);
13     while(!coll.empty())
14     {
15         cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
16         coll.pop();
17     }
18     return 0;
19 }

 

 

2.3 对于自定义类型,则必须重载operator<或者重写仿函数。

2.3.1 重载operator<的例子:返回true时,说明左边形参的优先级低于右边形参

 1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4 struct Node{
 5     int x, y;
 6     Node(int a=0, int b=0):
 7         x(a),y(b){}
 8 };
 9 bool operator<(Node a, Node b){//返回true时,说明a的优先级低于b
10     //x值较大的Node优先级低(x小的Node排在队前)
11     //x相等时,y大的优先级低(y小的Node排在队前)
12     if( a.x== b.x ) return a.y> b.y;
13     return a.x> b.x; 
14 }
15 int main(){
16     priority_queue<Node> q;
17     for( int i= 0; i< 10; ++i )
18     q.push( Node( rand(), rand() ) );
19     while( !q.empty() ){
20         cout << q.top().x << ' ' << q.top().y << endl;
21         q.pop();
22     }
23     return 0;
24 }

 

自定义类型重载operator<后,声明对象时就可以只带一个模板参数

但此时不能像基本类型这样声明priority_queue<Node,vector<Node>,greater<Node> >,原因是greater<Node>没有定义,如果想用这种方法定义则可以重载operator >。

例子:返回的是小顶堆。但不怎么用,习惯是重载operator<。

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 struct Node{
 5     int x, y;
 6     Node( int a= 0, int b= 0 ):
 7         x(a), y(b) {}
 8 };
 9 bool operator>( Node a, Node b ){//返回true,a的优先级大于b
10     //x大的排在队前部;x相同时,y大的排在队前部
11     if( a.x== b.x ) return a.y> b.y;
12     return a.x> b.x; 
13 }
14 int main(){
15     priority_queue<Node,vector<Node>,greater<Node> > q;
16     for( int i= 0; i< 10; ++i )
17     q.push( Node( rand(), rand() ) );
18     while( !q.empty() ){
19         cout << q.top().x << ' ' << q.top().y << endl;
20         q.pop();
21     }
22     return 0;
23 }

 

2.3.2 重写仿函数的例子(返回值排序与2.3.1相同,都是小顶堆。先按x升序,x相等时,再按y升序):

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 struct Node{
 5     int x, y;
 6     Node( int a= 0, int b= 0 ):
 7         x(a), y(b) {}
 8 };
 9 struct cmp{
10     bool operator() ( Node a, Node b ){//默认是less函数
11         //返回true时,a的优先级低于b的优先级(a排在b的后面)
12         if( a.x== b.x ) return a.y> b.y;      
13         return a.x> b.x; }
14 };
15 int main(){
16     priority_queue<Node, vector<Node>, cmp> q;
17     for( int i= 0; i< 10; ++i )
18     q.push( Node( rand(), rand() ) );
19     while( !q.empty() ){
20         cout << q.top().x << ' ' << q.top().y << endl;
21         q.pop();
22     }
23     return 0;
24 } 

 

 

参考:http://www.cnblogs.com/flyoung2008/articles/2136485.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154473.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • rammap使用_查看内存的命令

    rammap使用_查看内存的命令参考TechnetProcessPrivate:分配给单一Process专用的内存MappedFile:用来储放档案内容快取(Cache)的内存空间SharedMemory:标注给多个Process共用的内存分页(Page,内存管理单位)PageTable:用来描述虚拟内存位址的分页表(裡面是一笔一笔的PTE,PageTableEntries)PagedPool…

    2022年10月23日
    0
  • phpstorm2021 永久激活码【永久激活】

    (phpstorm2021 永久激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    572
  • 汉罗塔问题的递归实现「建议收藏」

    汉罗塔问题的递归实现「建议收藏」#includeusingnamespacestd;voidmove(intm,chara,charb);voidhanoi(intm,charone,chartwo,charthree);intmain(){  chararray[10]={‘A’,’B’,’C’,’D’,’E’,’F’};  hanoi(4,’A’,’B’,’C’);

    2022年10月12日
    0
  • 用js写简单选项卡

    用js写简单选项卡如图,最简单的纯粹的选项卡第一步,当然是先写html代码和css样式<!DOCTYPEhtml><html><head><metacharset=&quo

    2022年7月2日
    26
  • nginx负载均衡的5种策略及原理

    nginx负载均衡的5种策略及原理nginx的upstream目前支持的5种方式的分配1、轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 upstreambackserver{ server192.168.0.14; server192.168.0.15; } 2、指定权重指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 upst…

    2022年6月29日
    50
  • 阿里云如何申请免费ssl证书_https证书部署

    阿里云如何申请免费ssl证书_https证书部署本文SSL证书相关申请管理员扫描微信公众号方式登录:https://cloud.tencent.com工作台->搜索SSL证书->申请免费证书部署申请完毕后,在列表下载证书,解压后找到tomcat文件夹,有两个文件keystorePass.txt存放秘钥,www.njpingpang.com.jks为证书。把www.njpingpang.com.jks证书放入服务器tomcat/conf文件夹下。更改server.xml文件keystorePass:keystore

    2022年9月10日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号