priority_queue 自定义比较函数

 priority_queue 自定义比较函数

#include 
#include 
#include 
#include 
using namespace std;
struct Node{
    int x;
    int y;
    Node(int a=0, int b=0)
    {
        x = a;
        y = b;
    }
};
struct cmp{
    bool operator () (Node a, Node b)
    {
        if(a.x == b.x) return a.y > b.y;
        else return a.x < b.x ;
    }
};
/*
struct cmp{
    bool operator() ( Node a, Node b )
    {
        if( a.x== b.x ) return a.y> b.y;
        return a.x> b.x;
    }
};*/
int main()
{
    priority_queue , cmp > q;
    for(int i= 0; i< 10; ++i )
    {
        q.push(Node(i,10-i));
    }
    while( !q.empty())
    {
        cout << q.top().x << ' ' << q.top().y << endl;
        q.pop();
    }

    return 0;
}

 

你可能感兴趣的:(priority_queue 自定义比较函数)