首页 > 编程语言 > 详细

以优先级队列为例重写比较器 C++

时间:2020-07-13 09:59:14      阅读:75      评论:0      收藏:0      [点我收藏+]

优先级队列

template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;

仿函数

#include <vector>
#include <memory>

class Node{
public:
    int value;
    int row;
    int col;
    Node(int value, int row, int col){
        this->value = value;
        this->row = row;
        this->col = col;
    }
};
using NodePtr = shared_ptr<Node>;

struct heapComp{
    bool operator()(const NodePtr &a, const NodePtr& b)const {
        return a->value > b->value;  //从左到右由大变小---小根堆
    }
};

priority_queue<NodePtr, vector<NodePtr>,heapComp> heap;

heap.push(make_shared<Node>(1,2,3));
heap.push(make_shared<Node>(4,2,3));
cout<<heap.top();

以优先级队列为例重写比较器 C++

原文:https://www.cnblogs.com/zhilong233/p/13291659.html

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