自定义`priority_queue`排序方式

自定义priority_queue排序方式

参考:优先队列(priority_queue)四种自定义排序方法

一种方法是定义全局的重载函数,另一种方法是自定义一个结构体,然后重载函数。

pair只能用第二种方法。

// Created by CAD on 2020/5/15.
#include <bits/stdc++.h>
using namespace std;

struct cmp{
    bool operator()(int a,int b){
        return a<b;
    }
};

int main() {
    priority_queue<int,vector<int>,cmp> q;
    //升序取最小,降序取最大
    q.push(1),q.push(2),q.push(3);
    cout<<q.top();
    //输出为3
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/CADCADCAD/p/12893894.html