Sign in to follow this  
ViperG

Sorting a Struct pointer vector

Recommended Posts

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());

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Standard library containers dynamically allocate memory (and manage it) *for* you. That's a large part of the reason for using them. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this