C++STL的priority_queue用法总结

翻了很多博客的总结

1、头文件

#include<queue>


2、定义

[cpp]  view plain  copy
  1. priority_queue<int> p;  


3、优先输出大数据

priority_queue<Type, Container, Functional>

Type为数据类型, Container为保存数据的容器,Functional为元素比较方式。

如果不写后两个参数,那么容器默认用的是vector,比较方式默认用operator<,也就是优先队列是大顶堆,队头元素最大。


例如:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<queue>  
  3. using namespace std;  
  4.   
  5. int main(){  
  6.     priority_queue<int> p;  
  7.     p.push(1);  
  8.     p.push(2);  
  9.     p.push(8);  
  10.     p.push(5);  
  11.     p.push(43);  
  12.     for(int i=0;i<5;i++){  
  13.         cout<<p.top()<<endl;  
  14.         p.pop();  
  15.     }  
  16.     return 0;  
  17. }  


输出:





4、优先输出小数据

方法一:

[cpp]  view plain  copy
  1. priority_queue<int, vector<int>, greater<int> > p;  


例如:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<queue>  
  3. using namespace std;  
  4.   
  5. int main(){  
  6.     priority_queue<int, vector<int>, greater<int> >p;  
  7.     p.push(1);  
  8.     p.push(2);  
  9.     p.push(8);  
  10.     p.push(5);  
  11.     p.push(43);  
  12.     for(int i=0;i<5;i++){  
  13.         cout<<p.top()<<endl;  
  14.         p.pop();  
  15.     }  
  16.     return 0;  
  17. }  


输出:




方法二:自定义优先级,重载默认的 < 符号


例子:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<queue>  
  3. #include<cstdlib>  
  4. using namespace std;  
  5. struct Node{  
  6.     int x,y;  
  7.     Node(int a=0, int b=0):  
  8.         x(a), y(b) {}  
  9. };  
  10.   
  11. struct cmp{  
  12.     bool operator()(Node a, Node b){  
  13.         if(a.x == b.x)  return a.y>b.y;  
  14.         return a.x>b.x;  
  15.     }  
  16. };  
  17.   
  18. int main(){  
  19.     priority_queue<Node, vector<Node>, cmp>p;  
  20.       
  21.     for(int i=0; i<10; ++i)  
  22.         p.push(Node(rand(), rand()));  
  23.           
  24.     while(!p.empty()){  
  25.         cout<<p.top().x<<' '<<p.top().y<<endl;  
  26.         p.pop();  
  27.     }//while  
  28.     //getchar();  
  29.     return 0;  
  30. }  


输出:


#include <iostream>

#include <queue>

#include <string>

using namespace std;

struct package

{ int id;

string data;

};

bool operator<(package a, package b)

{ return a.id < b.id;} //自定义排序规则

int main()

{ priority_queue<package> tmp;

tmp.push({3,"a"});

tmp.push({2,"b"});

int size = tmp.size();

while(size--)

    { cout << tmp.top().id << " " << tmp.top().data <<endl;

tmp.pop();

}

}

以结构体Time为例:

[cpp]  view plain  copy
  1. struct Time{  
  2.     int start, end;
  3. };  


使用优先队列时,如果需要对Time中的start从小到大排序,有两种方法:

[cpp]  view plain  copy
  1. priority_queue<Time> pq;  


一.在结构体外重载结构体小于运算符:
[cpp]  view plain  copy
  1. bool operator <(const Time& a,const Time& b){  
  2.     return a.start > b.start;  
  3. }  //这里以大于重载小于是因为默认情况下,优先队列是以大的作为队首,这样一反,就可以再默认情况下使得小的作为队首  

二.直接在结构体中重载小于运算符:
[cpp]  view plain  copy
  1. struct Time{    
  2.     int start, end;    
  3.     bool operator < (const Time& t)const{    
  4.         return start > t.start;    
  5.     }    
  6. };    

实质上来说是一样的。。。。

另外要注意的是:参数列表中的const不能省略,否则报错~~



默认的优先队列是个极大堆,如果要改变优先队列中元素的优先级,有下面这些方法

[cpp]  view plain  copy
  1. struct cmp1    
  2. {      
  3.     bool operator ()(int &a,int &b)    
  4.     {      
  5.         return a>b;//最小值优先       
  6.     }      
  7. };      
  8. struct cmp2    
  9. {      
  10.     bool operator ()(int &a,int &b)    
  11.     {      
  12.         return a<b;//最大值优先       
  13.     }      
  14. };      
  15. struct node1    
  16. {      
  17.     int u;      
  18.     bool operator < (const node1 &a) const     
  19.     {      
  20.        return u>a.u;//最小值优先       
  21.     }      
  22. };      
  23. struct node2    
  24. {      
  25.     int u;      
  26.     bool operator < (const node2 &a) const     
  27.     {      
  28.         return u<a.u;//最大值优先       
  29.     }      
  30. };     
  31.     
  32. priority_queue<int>q1;//采用默认优先级构造队列         
  33. priority_queue<int,vector<int>,cmp1>q2;//最小值优先       
  34. priority_queue<int,vector<int>,cmp2>q3;//最大值优先       
  35. priority_queue<int,vector<int>,greater<int> >q4;//注意“>>”会被认为错误,       
  36.                                                 //这是右移运算符,所以这里用空格号隔开,最小值优先     
  37. priority_queue<int,vector<int>,less<int> >q5;//最大值优先        
  38. priority_queue<node1>q6;  //自定义优先级    
  39. priority_queue<node2>q7;  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值