-
Priority_queue
How do I go about adding a comparator to a priority queue. I have a class which Im using as my Type for the queue, and want to order the class elements according to my own comparator. Is there a method I have to add to my class that the priority_queue will automatically use?
-
If you make an operator< for your class that sorts based on priority the priority_queue will use it automatically.
Code:
bool operator<(const myclass&, const myclass&);
If there are multiple correct priority orders you should make structs whose operator() has the same arguments as an operator<. Then you will have to pass the correct struct name to the priority_queue template parameters.
-
Still having some trouble with the queue. I've implemented operator overloading in the class, and it works fine, so I know thats not the problem. For some reason though, Im getting an error when I try to store an instance of my class in the priority_queue.
I have the queue declared in my header
Code:
#inlclude <queue>
class myClass
{
public:
...
private:
std::priority_queue<myOtherClass> myQueue;
};
Yet when I make the call to push(myOtherClass) onto the queue, I get an obscure error originating in stl_heap. The thing is, I dont get the error if I store pointers in the queue, rather than actual instances of myOtherClass.
Can anyone explain?
-
How is operator< defined for your myOtherClass?
-
Ok. After thinking this through. I assume its working with pointers because the queue is no longer using my operator overloading to compare the values, yes?
Which still leaves me having no clue as to why it doesn't work with my class instances, since testing the overloading using cout gives the correct answer each time, so Im sure thats working ok.
-
Code:
bool AStarNode::operator<(AStarNode &m)
{
if(f != m.getF())
return f < m.getF();
else
return h < m.getH();
}
-
Hmm... for starters it's probably best to change that parameter to const AStarNode &m. You should always have them const unless you can't. Read my sig :) and here: http://www.parashift.com/c++-faq-lit...rrectness.html.
EDITED:
Changing to a reference to const should be enough
-
By god it works.. it WORKS....
I've come to the conclusion that the problem was a mixture of two things. The first being my lack of knowledge of c++, and the way in which operator overriding must be done, and the second is my installation of Dev-C++ seems to have been a little funky.
I've just installed Borland, and after changing the operator override to an external friend function, and giving it the two parameters, its now working. My queue is storing the instances of my class, and ordering them correctly. I did try doing it in Dev, but it just wasnt having any of it... ah well... Thanks for the help.
-
It's probably not Dev-C++'s fault (actually the gcc compiler), it is probably your code. It would be smart to try to fix the problem that shows up in Dev-C++ rather than ignore it. It might be a problem on Borland as well, but one that shows up somewhere else. It might also cause you problems in the future if you don't learn the right way to do things.
The const is important. If you make the operator a member function, not only do you have to make the parameter a const reference, you have to make the function const as well. I'd prefer the non-member function I gave an example of earlier, though.