首页 > 编程语言 > 详细

自定义`priority_queue`排序方式

时间:2020-05-15 12:07:18      阅读:59      评论:0      收藏:0      [点我收藏+]

自定义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;
}

自定义`priority_queue`排序方式

原文:https://www.cnblogs.com/CADCADCAD/p/12893894.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!