0

I am new to C++ and I am trying to use the STL priority queue to make a min heap of objects based of off the distance property in the object. I am not fully sure what I need to do but I've looked around and I that my priority queue would look something like this:

std::priority_queue<Class, std::vector<Class>, object.distance> pq;

What I am not fully sure about is how I go about overloading the comparison operator first to change the heap to a min heap and second to compare my objects based off of a property. Any help would be appreciated?

user2604504
  • 697
  • 2
  • 14
  • 29

1 Answers1

1

Note that it is the heap opperations that take the comparators, not your contianer.
Presuming that your function has this prototype: static bool object::distance( Class, Class );
Then you can use the std::make_heap function, like this std::make_heap( pq.begin(), pq.end(), object::distance )

It's probably worth noting that a std::priority_queue doesn't have a begin or end method and doesn't provide the RandomAcessIterators that you need for the STL's heap functions. The fact that you are trying to make a heap out of your queue indicates that you are missusing the container. A priority_queue already has it's own ordering. If you want to use a heap you can just use a vector, like this: std::vector< Class > pq;

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288