您的当前位置:首页正文

pair实现priority_queue

2024-12-02 来源:个人技术集锦

只需要自定义比较函数即可!

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
class mycomparison
{
    public:
        bool operator () (const pii &n1, const pii &n2) const
        {
            if(n1.first == n2.first)
                return n1.second > n2.second;
            return n1.first  > n2.first;
        }

};
int main()
{
    priority_queue<pii, vector<pii>, mycomparison> q;
    q.push(pii(3, 4));
    q.push(pii(5, 6));
    q.push(pii(5, 4));
    while(!q.empty())
    {
        cout << q.top().first <<  " " << q.top().second << endl;
        q.pop();
    }
    return 0;
}


显示全文