Sorting a Struct pointer vector

Started by
7 comments, last by GameDev.net 17 years, 8 months ago

struct planetbody
{
	int id, sorbit;
	float x,y, angle; 

	const bool operator< (const planetbody& rhs) const {
	return sorbit < rhs.sorbit;}

};

struct solsystem
{
	float x,y;
	float r, g, b;

	vector<planetbody*>Planets;

};

extern vector<solsystem*> Systems;



is this right? it isn't sorting at all using

sort(Systems[x]->Planets.begin(),Systems[x]->Planets.end());

Black Sky A Star Control 2/Elite like game
Advertisement
No - since the vector has a pointer value type, it won't "see" the operator<. You need to specify a custom comparator (ie, a predicate) to the sort function to make sure they're sorted properly, and not just my memory address.

Generic Algorithm Tutorial

You'd want something that looked like this -

class Predicate {public:     bool operator()( const planetbody& lhs, const planetbody& rhs ) {          return ( lhs->sorbit < rhs->sorbit );     }};


And then call your sort function like -

sort( Systems[x]->Planets.begin(), Systems[x]->Planets.end(), Predicate() );

See if that does the trick. I honestly haven't used predicates in a long while :O

i hate writing little predicates(true or false functions). Is there a way you can do it by using function adaptors?
Why do you need pointers to the info? I've found it to be very poor performace wise. Could just be my implementation. Also, its a bit pointless for something like this.
Try std::mem_fun(&planetbody::operator<). (std::mem_fun wraps functions that operate on pointers to objects; std::mem_fun_ref likewise for objects passed by reference.) If that doesn't work, you'll need to either look into boost::bind, or do it yourself.

But first, make really sure that storing a vector of pointers is really what you want to do. The standard library containers are intended to store things by value (unless you have a specific reason to add the indirection).
I just thought it would be better to store everything as pointers to save space etc.

I am programing using visual studio 2005. But back in high school when i took turbo pascal, we learned that the stack had limited memory, and so we had to use dynamic allocated memory if we wanted anything usefull.
Black Sky A Star Control 2/Elite like game
Standard library containers dynamically allocate memory (and manage it) *for* you. That's a large part of the reason for using them. :)
In general, I avoid overloading operator<() just for sorting.

The main reason is that it is like driving a nail with a sledgehammer. In your case, does it really make sense that one planet is less than another planet? What does it mean? The < operator is well-defined for numbers, but not well-defined for planets.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by ViperG
I just thought it would be better to store everything as pointers to save space etc.

I am programing using visual studio 2005. But back in high school when i took turbo pascal, we learned that the stack had limited memory, and so we had to use dynamic allocated memory if we wanted anything usefull.


Like Zahlman said, it IS dynamically allocated when you use the standard library like vector etc. You're just adding another layer of pain to your life when you have to clean it up.

This topic is closed to new replies.

Advertisement