This site suggests that if I want to reverse-order my priority queues, the following code is what I should use:
#include <iostream>
#include <queue>
using namespace std;
class mycomparison{
bool reverse;
public:
mycomparison(const bool &revparam=false) {reverse=revparam;}
bool operator() (const int &lhs, const int &rhs) const {
if (reverse) return (lhs>rhs);
else return (lhs<rhs);
}
};
int main (){
int myints[]= {10,60,50,20};
priority_queue<int, vector<int>, mycomparison(true)> first;
return 0;
}
This bothers me:
Is there a more elegant or less verbose way of reverse-sorting a priority queue?
You can't avoid specifying the storage container, but you can avoid writing your own functor:
priority_queue<int, vector<int>, std::greater<int> > first;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With