首页 > 其他 > 详细

priority_queue优先队列中定义自己的比较算子

时间:2020-03-21 01:27:04      阅读:109      评论:0      收藏:0      [点我收藏+]
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int x,y,z;
 5     node(int a,int b,int c):x(a),y(b),z(c){}
 6     bool operator < (const node &rhs) const{
 7         return x < rhs.x;//按照x来定义优先顺序 
 8     }
 9 };
10 int main(){
11     priority_queue<node> q;
12     q.push(node(3,4,5));
13     q.push(node(4,5,6));
14     q.push(node(2,3,4));
15     while(!q.empty()){
16         node t = q.top();
17         q.pop();
18         cout << t.x << " " << t.y << " " << t.z << endl;
19     }
20     return 0;
21 }
22 /*
23     > 
24     结果:
25         2 3 4
26         3 4 5
27         4 5 6
28     <
29     结果:
30         4 5 6
31         3 4 5
32         2 3 4
33     结论:
34         priority_queue中重载运算符与sort中重载运算符刚好相反 
35 */

 

priority_queue优先队列中定义自己的比较算子

原文:https://www.cnblogs.com/zhangqiling/p/12535780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!