优先队列重载 3种写法

来源:互联网 发布:淘宝上如何申请换货 编辑:程序博客网 时间:2024/06/08 13:15

首先明确队列默认由大到小,由小到大可以为 priority_queue<int,vector<int>,greater<int> > q;

优先队列的符号是   <  重载时肯定也只能 重载   <     可以尝试 >    .....会报错哒;

给三种写法:


typedef struct node{    int pi;    int di;    bool operator < (const node &b)const    {        if(pi == b.pi)            return di > b.di;        else            return pi > b.pi;    }} stone;


typedef struct node{    int pi;    int di; } stone;
 bool operator < (stone a,stone b)
    {        if(a.pi == b.pi)            return a.di > b.di;        else            return a.pi > b.pi;    }






typedef struct node{    int pi;    int di;    friend bool operator < (struct node a,struct node b)    {        if(a.pi == b.pi)            return a.di > b.di;        else            return a.pi > b.pi;    }} stone;

2 0
原创粉丝点击