Boost priority queue comparison function

2019-04-02 12:30发布

问题:

How to change comparison function on a priority queue with boost library? I have a struct like this :

struct decreasingOrderMyType
{
    bool operator() (const MyType & lhs, const MyType & rhs) const
    {
        return lhs.value > rhs.value;
    }
};

and i would like use it to compare my elements.

thanks!

回答1:

For the std::priority_queue I have it specified as: std::priority_queue<DistanceTuple, std::vector<DistanceTuple>, SmallestOnTop > pq;

DistanceTuple is a std::pair and SmallestOnTop is a functor to compare the std::pair

Update: I was wrong, they are not identical. The boost version uses named parameters. Which work like this:

boost::heap::priority_queue<MyType,
                            boost::heap::compare<decreasingOrderMyType> > pq;