priority_queue自定義struct的優先級設置寫法

#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();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章