Skip to main content
gamedev.net
🔒 Locked

stl question

Started by WoX 5 replies 450+ views
Original Post
WoX
WoX
I was wondering how to sort a list of structs after a specific membervariable. Here''s some nice pseudocode #include <list> struct Node { int x, y; int f, g, h; Node *Parent; }; std::list NodeList; for(int i=0; i<10; i++) { Node node = {i,i,rand()%100,0,0,0}; NodeList.push_back(node); } there, now I wan''t to sort the list after the f membervariable in ascending order, if the list would have contained int''s instead of Node''s this would have worked: NodeList.sort(); or for descending order: std::greater great; NodeList.sort(great); Anyone here''s a guru on stl and can share som knowledge?? thanks for any help
hewhay
hewhay
Stl''s sort uses the operator < to sort the values. You can either change to a class and overload it as a member function or overload it like this

  
bool operator<(const Node & n1, const Node & n2)
{
return (n1.f<n2.f);
}
humanity will always be slaved by its ignorance
Fruny
Fruny
You need to either overload comparison operators for your new types or define an appropriate binary predicate that you then pass to sort.

Like

bool
OrderByH( const Node& lhs, const Node& rhs )
{
return lhs.h < rhs.h;
}

...

sort( List.begin(), List.end(), OrderByH );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny
Fruny
quote:
Original post by hewhay
Stl''s sort uses the operator < to sort the values. You can either change to a class and overload it as a member function or overload it like this



No, that''s the _default_ behaviour. It may be indesirable to define a ''global'' ordering criterion for a given type (e.g. complex numbers), while still being able to sort them in some cases (by real part or imaginary part).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
23yrold3yrold
23yrold3yrold
Instead of the binary predicate, it may be faster to use list''s own sort() function, and pass it a function object. That''s how I''d do this, anyway.

Chris Barry (crbarry at mts.net)
My Personal Programming Depot
Jesus saves ... the rest of you take 2d4 fire damage.
Fruny
Fruny
You''re right, you must use list''s sort() member function.
But the parameter isn''t necessarily a function object, it can be a binary predicate (i.e. a function taking 2 parameters and returning a bool).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Stoffel
Stoffel
PSA (Public Service Announcement):
std::list is the worst container you can sort. That''s why it has its own sort member function (rather than std::sort, which can be used on vectors and dequeus; maps and sets are always sorted, of course). If you''re sorting once, it''s probably OK. If you''re sorting a lot, you might want to look into a different container.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.