自定义`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;
}
    CAD加油!欢迎跟我一起讨论学习算法,QQ:1401650042

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号