优先队列重载 3种写法

首先明确队列默认由大到小,由小到大可以为 priority_queue,greater > 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;

你可能感兴趣的:(数据结构)