优先队列的排序写法

1.普通方法:

priority_queuequ;


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

2.自定义优先级:

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

priority_queue,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_queuequ;


你可能感兴趣的:(优先队列)