优先队列中排序写法

1.普通方法:

priority_queue<int>qu;


对入队的元素默认按照从大到小排序。

2.自定义优先级:

struct cmp{
  bool operator()(int x,int y)
{
    return x>y;   //从小到大排序。即x小的优先级高。
}
};
 
priority_queue<int,vector<int>,cmp>qu;


3.对结构体进行重载操作符:

struct node {
   int x,y;
   friend bool operator < (node a, node b)
   {
       return a.x>b.x;    //x小的优先级高。
   }
};


也可以:

struct node{
int x,y;
};
bool operator(const node &a,const node &b)
{
return a.x>b.x;
};
 
 
priority_queue<node>qu;
--------------------- 
作者:sprite_ 
来源:CSDN 
原文:https://blog.csdn.net/aaaaacmer/article/details/49474529 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/84942040