Nov 27, 2012 at 3:40am UTC
Hey All,
I need some help with the use of a Priority Queue.
I have a Struct
struct node
{
string typeItem;
int sizes[9];
int d;
int h;
int c;
};
Up until now this info has been pushed into a FIFO queue and all was working good using
queue.push(box);
Now I need to do the same thing except push the struct into a priority_queue using the value stored in
int c;
with the lowest value being the one on top and the one with higher values pushed down the line.
Example. Push in 18, 7, 23, 4, 12 and get out 4, 7, 12, 18, 23.
I have looked at the examples from a search on the internet but cant seem to make this happen.
Anyone know the syntax for what I am trying to do?
Nov 27, 2012 at 1:06pm UTC
You just need to an an operator< to your class/struct
then you can use the container
http://www.cplusplus.com/reference/queue/priority_queue/
unfortunately the container is coded to give max heap at the top
One option is to cheat and code you operator< in reverse i.e.
1 2 3 4
int node::operator <(const node& other)
{
return c > other.c;
}
or you could write a comparison object
Last edited on Nov 27, 2012 at 1:06pm UTC
Nov 27, 2012 at 1:39pm UTC
mik2718,
I looked at the link you provided last night.
That is what I need to do but the page reads like a foreign language to me.
I tried to set up
priority_queue <node, vector<int>, greater<int> > pQueue (node.c);
The word
node.c
gets underlined in red and gives me the message
Error: a non-static member reference must be relative to a specific object.
I have no idea how to fix.
Last edited on Nov 27, 2012 at 1:40pm UTC
Nov 27, 2012 at 3:43pm UTC
I really need something for this. Anybody got a clue?
Nov 27, 2012 at 4:10pm UTC
I added the above as you suggested.
At the end of what is line 9 above, there is a red curly line under the
)
and it says
Error: Expected a ';'
Also there is an open curly brace on line 9 that has no closed curly brace. Did it get cut off?
Nov 27, 2012 at 5:37pm UTC
I think we are on the right track.
now I get
error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const node' (or there is no acceptable conversion
could be 'int node::operator <(const node &)'
while trying to match the argument list '(const node, const node)'
I looked up the error but it is way out of my league.
Nov 27, 2012 at 5:55pm UTC
It is compiling correctly now. I will follow up.
Nov 30, 2012 at 8:59pm UTC
This worked you guys are great.
Dec 1, 2012 at 4:59am UTC
I suppose you have also reasoned out why the comparison predicate must be const-correct.
Dec 2, 2012 at 2:33am UTC
Not sure there was ever an issue with the const part. It was the syntax that was throwing me. When I look at the documentation, in my mind I cannot connect what they show with what it actually ended up being. When I look at what you and mik2718 showed me it made perfect sense. In my work I very seldom have to write code. When I do and run across something new, I have to rely on the documentation. If it is bad such as this I have to find an example or rely on good people like yourself to break it down for me.