重载结构体运算符

优先队列重载结构体的运算符常见的方法:

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;

 

struct node{
    int x;
    int y;
}point;

bool operator<(const node &a,const node &b)
{
    return a.x>b.x;
}

priority_queue<node> Q;


语法:
<返回类型> 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;
posted @ 2020-01-15 13:36  北冥有鱼兮  阅读(322)  评论(0编辑  收藏  举报