I’m wanting to do something like this:
priority_queue< pair<int, int>, vector<int>, greater<int> > Q;
This works fine if the type I’m comparing is int, i.e.:
priority_queue< int, vector<int>, greater<int> > Q;
however, obviously with the pair<int, int>, there is no way of comparing the pairs in the queue with the standard >. I was wondering what I should do? How would I implement an overloaded > or is there another way I can create a priority queue of pairs with the smallest pair.second being at the top of the queue?
Have you tried this?
This will give the reverse ordering of the normal
operator<forpair<int, int>, which will start with the smallestfirsttie-broken with smallestsecond.If you want to sort by smallest
secondfirst andfirstsecond (!) then you’ll need a new sorting functor:Then use: