以优先队列为例,从cppreference查得,它的模板为

template<

    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>

> class priority_queue;

所以我们要定义一个Compare类实现less的功能,在观察less类的内容,https://en.cppreference.com/w/cpp/utility/functional/less

std::less::operator()
bool operator()( const T& lhs, const T& rhs ) const;(until C++14)
constexpr bool operator()( const T& lhs, const T& rhs ) const;(since C++14)
Checks whether lhs is less than rhs.
Parameters
lhs, rhs    -    values to compare
Return value
true if lhs < rhs, false otherwise.
constexpr
bool operator()(const T &lhs, const T &rhs) const { return lhs < rhs; }

实现非常简单,只要对我们自定义的类实现operator()重载即可

posted on 2018-09-07 01:34  bloomingFlower  阅读(175)  评论(0编辑  收藏  举报