优先队列的重载运算符


大家都知道

优先队列是个好东西

但它怎么如同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;

 

 

 

暂时这样吧

以后可能还会补充

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM