Hi,
I have an exercise to create a priority queue solely using an std::vector container from STL. The PQ itself works fine, but there are two associated problems left which I don't know how to solve...
We've been given a file containing the main() which does the following thing:
Code:
#include "priorityqueue.h"
template<class Type>
class LessThan {
public:
bool operator()(const Type& el1, const Type& el2) { return el1 < el2; }
};
int main() {
PriorityQueue< std::string > h;
h.push(..); // a few times
std::cout << h.pop() << std::endl; // for all the elements in the queue
PriorityQueue<std::string, LessThan<std::string> > h;
// like above
}
And I created the necessary class in "priorityqueue.h" like this:
Code:
template<typename T, class Compare>
class PriorityQueue {
// ...
class BinaryHeap {
friend class PriorityQueue
// ...
};
};
The second PQ from the main will work fine like this, but with the first it (logically) gives the error "error: wrong number of template arguments (1, should be 2)". Is there a way to put the "class Compare" to the default compare operator when the second argument isn't given when the PQ is defined?
Now, the PQ works like it should when I give the LessThan class along when defining the queue. The thing I wonder is whether I still need to use the class in some way to overload the compare operator, or will this automatically be done by the compiler? Meaning that when a function of the nested BinaryHeap class compares two elements in the heap, it will use the overloaded operator.
I hope I'm a bit clear and understandable. I'm never any good at explaining problems in programming, especially in English .
Thanks,
Edward.