![]() |
STL priority queue structure question
I am trying to use a PQ with a structure, VERTEX.
Here is the code for VERTEX: :
struct edgeI am have several problems with using VERTEX in my priority queue. 1.) I need to write several functions to overload my ==. = and > operators. I believe I have done this correctly, but I am not too sure, I also cannot seem to write the '=' one. 2.) When I do the declaration priority_queue<VERTEX> pq; it compiles. However, I begin having problems when I push elements into the que, for example: The vertex information before the push is: :
displayVertex(vertList[0]);Then, when I do the following: :
vert = pq.top();Vert has 0 edges, with a cost of: 0. vertList is a list of the vertices, in an array. I have checked the contents of this before pushing the elements and they are correct. It seems that somehow, something is being lost and I am not entirely sure why, or how. If anyone could explain this to me, that'd be great. Thank you for any of your help, I have included the code under this so you can look at it. It has been cleaned up, so it should be easy to read. Thanks :
//a program |
I haven't really looked at your post in detail, because I'm cooking. I can recommend this for material on overloading operators. Very good.
|
I am not using classes though and I'm not entirely sure how to do it with structures, and then have the PQ recognize that they are overloaded, or in general.
I have tried to do as described above, then have a > b, but that doesn't seem to be working either. If you have any websites for structures, that'd be great! |
In C++, a structure and a class are the same thing. The difference is that class members are, by default, private. Structure members are, by default, public. The fact that you haven't written any "methods" for the structure is totally immaterial to the underlying situation.
|
I havent checked it all just what I think is going wrong.
The compiler will always try to generate the standard operators for you, unless you define your own. With a simple POD (plain old data) structure like yours the compiler generated = operator should work fine. But you have defined your own = operator that does nothing, so bits in your code like this: :
vert = pq.top();are just using the empty operator and nothing is really being copied. Either have another look at the article Dawei posted or get rid of your = operator and let the compiler do it for you. |
DOH!!!!
Thank you to the both of you. I removed the one I wrote and it works now LOL. I can go and finish up the rest of the program now. I am thankful that it was something stupid that was occuring. I will try to play around with structures in C++ since you said it doesn't matter. I find that very interesting. Thank you again guys. I'll post again if I have problems. I do have another quick question - how do I make the que sort from least to most? In the default mode it is from highest to lowest. Thanks |
| All times are GMT -5. The time now is 10:31 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC