previous | start

Sort Algorithm

STL also has many template functions which implement traditional and useful algorithms. These template functions are reusable since the type of the elements they can be applied to is parametrized and can be specified simply by passing a given type to the function.

For example, STL has a sort function:

#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  vector<string> v; 
  // Store values in v
  ...
  // Now sort strings in v in usual order

  sort(v.begin(), v.end());
  ...
}

The same sort statement works with no change if v is a vector of some other type such as int.



previous | start