Reputation: 4545
I am attempting to create a class that takes a priority queue and reverses the priorities. I include a bool operator<()
function as a member function of the class, but no matter how I structure this function, the operator never seems to be overloaded.
Here is my class:
template<typename T>
class MinPQ{
public:
bool empty() const {
return pq.empty();
}
unsigned int size() const {
return pq.size();
}
void push(const T& element){
pq.push(element);
}
const T& min() const {
return pq.top();
}
void remove_min(){
pq.pop();
}
bool operator<(const T& element) const {
return pq < element;
}
private:
priority_queue<T> pq;
};
EDIT
I also tried changing the overloading function to the following and do not undertsand why that isn't working either.
bool operator<(const T& element){
return this < element;
}
Upvotes: 0
Views: 72
Reputation: 481
Your operator<
is meaningless. The only thing it can do is comparing MinPQ<T>
object with T
object. If you want to compare two T
objects you should do this:
1) If the template type T
can take only a handful of types, you can write comparison operators explicitly for each of these types:
bool operator<(const T& lhs, const T& rhs)
{
return lhs > rhs;
}
note: each operator<
should be a non-member function. Or a member function of class T
with a single argument.
2) write a comparison functor:
template <class T> struct CompareT
{
bool operator()(const T& lhs, const T& rhs) const
{
return lhs > rhs;
}
};
and then declare pq
member as follows:
priority_queue<T, std::vector<T>, CompareT<T>> mq;
3) If you want simply to inverse priorities, you could simply use std::greater
class:
priority_queue<T, std::vector<T>, std::greater<T>> mq;
Upvotes: 1