优先队列自定义优先级

#include<iostream>
#include<queue>
using namespace std;
struct node{
    int x;
    int y;
    friend 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<int,vector<int>,greater<int> > que1;// 数字越小优先级越高
    priority_queue<int,vector<int>,less<int> > que2;// 数字越大优先级越高
    priority_queue<node> que;// 自定义结构体优先队列优先级
    node now1={2,3};
    node now2={1,4};
    node now3={5,11};
    node now4={2,4};
    node now5={2,5};
    que.push(now1);
    que.push(now2);
    que.push(now3);
    que.push(now4);
    que.push(now5);
    while(!que.empty())
    {
        node k=que.top();
        cout<<k.x<<" "<<k.y<<endl;
        que.pop();
    }
}

猜你喜欢

转载自www.cnblogs.com/Leozi/p/10835568.html
今日推荐