Dave's Brain

Browse - Programming Tips - What's up with sorting in STL?

Date: 2008oct1
Language: C/C++
Keywords: Standard Template Library

Q.  What's up with sorting in STL?

A.  I am sure there is a reason but it seems inconsistent to me.

To sort a vector:

#include <vector> #include <algorithm> std::vector<int> a; a.push_back(3); a.push_back(2); a.push_back(1); std::sort(a); // Use sort() from algorithm!
To sort a list:
#include <list> std::list<int> b; b.push_back(3); b.push_back(2); b.push_back(1); b.sort(); // Use the sort member!
Perhaps this is because its easy to reorder elements in a list but not a vector.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.