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:
- I have to specify the storage class in my constructor.
- I have created a class whose only purpose is to be passed to the priority queue.
Is there a more elegant or less verbose way of reverse-sorting a priority queue?
priority_queue<int, vector<int>, mycomparison> first(true)
; – Palimpsest