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

优先级队列

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();
posted @ 2020-07-13 09:44  阿龙233  阅读(463)  评论(0编辑  收藏  举报