Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

psykr

sorting an std::list?

This topic is 5452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an std::list of ObjectIDs, and I want to add ObjectIDs to the list one by one. Is there any way that I can specify that I want the items inserted to be sorted, short of doing it by hand?

Share this post


Link to post
Share on other sites
Advertisement
Oops it is supposed to be std:: priority_queue. I quess "; p" when typed without the space between them becomes the smiley -

Share this post


Link to post
Share on other sites
Sure, there is a sort algorithm in the standard libraries. You specify a comparison function. Search for stl sort in google, you''ll find all the info you need, and better than I can describe it.

Share this post


Link to post
Share on other sites
If the items in the list are Objects of a class, you can also overload the > operater and then you can call theList.sort() and it will sort it.

Share this post


Link to post
Share on other sites
quote:
Original post by Ataru
Sure, there is a sort algorithm in the standard libraries. You specify a comparison function. Search for stl sort in google, you''ll find all the info you need, and better than I can describe it.


This doesn''t work for lists. std::sort needs a random access iterator. A list has it''s own sort function: list.sort()


My Site

Share this post


Link to post
Share on other sites
You''re right, I only ever use vectors for automatic sorting, but that makes complete sense. Sorry for misleading.

Share this post


Link to post
Share on other sites
quote:
Original post by Brad8383
If the items in the list are Objects of a class, you can also overload the > operater and then you can call theList.sort() and it will sort it.


What if I''m sorting pointers? For example, std::list<Object*>? If I overloaded the > operator, would it still sort? Sorry, no access to a C++ compiler lately.

Share this post


Link to post
Share on other sites
You can create a binary predicate function (or functor) and pass the function to sort instead of overloading < operator.

Example:

#include <list>
#include <functional>

class Object {
//...
};

//function
bool my_compare(Object * o1, Object * o2) {
if (o1->whatever < o2->whatever) return true;
return false;
}

//functor
class my_compare_f {
public:
my_compare_f() : n(0) {}
bool operator()(Object * o1, Object * o2) {
n++; return my_compare(o1, o2);
}
int n;
};

//...
std::list<object *> myList, myList2;
//...

myList.sort(my_compare);
my_compare_f c;
myList2.sort(c);
//c.n holds number of comparisons

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!