优先队列的重载运算符

大家都知道

优先队列是个好东西

但它怎么如同sort一样

自定义比较方式呢

这里就献上几种

重载运算符的方法

First

如果对象是int

STL默认是大根堆

只需要

priority<int> Q

↓↓↓

priority<int,vector<int>,greater<int> > Q

它就能摇身变为小根堆

so easy 过

Secondly

重点是结构体的重载

隆重推出

operator

给出四种方法吧(都是等价的)

struct node{
    int x;
    int y;
    friend bool operator<(const node a,const node b)
    {
        return a.x>b.x;
    }
};
priority_queue<node> Q;
struct node{
    int x;
    int y;
    bool operator<(const node &a) const
    {
        return x>a.x;
    }
};
priority_queue<node> Q;
struct node{
    int x;
    int y;
}point;
bool operator<(const node &a,const node &b)
{
    return a.x>b.x;
}
priority_queue<node> Q;
struct node{
    int x;
    int y;
};
struct cmp{
    
    bool operator()(node a,node b){
        return a.x>b.x;
    }
};
priority_queue<node,vector<node>,cmp> Q;

暂时这样吧

以后可能还会补充

原文地址:https://www.cnblogs.com/zhouzhihao/p/10974594.html