STL Question.

Started by
3 comments, last by mrmrcoleman 20 years, 1 month ago
In a program I am working on I need to be able to maintain a list of objects. i.e. to be able to add, remove, and sort them. I have read that the standard template library can do this for me but I am a bit lost when it comes to sorting the lists. I have done something similar to this is Java whereby you just make sure that the objects in the list have a comparison method implemented and then the container does the rest, but this doesnt seem to be the case with STL? Could anybody give me a brief introduction in plain english so that I can get my head round this?? Thank you for any help. Mark Coleman
Advertisement
It''s not part of std::list, but instead a function which works on arrays or lists.

SGI''s documentation for sort

Also, if the sorting is a very important or frequently needed aspect (like if you''re doing lots of dynamic object additions), you could put them into a std::set, which DOES automatically sort them by storing them in a binary search tree (usually a red-black tree).
sort() is a member function of std::list. Some compilers ship with broken copies of std::list::sort(), however. std::sort() sorts a range defined by random acess iterators, so can only be used with certain containers such as std::vector or std::deque (not std::list).
A std::map is also always sorted.

This topic is closed to new replies.

Advertisement