sort(), stable_sort()

By default, sorts the elements in ascending order using the less-than operator. An optional third parameter allows us to pass in an alternative ordering operation. stable_sort() preserves the relative order of elements within the container. For example, imagine that we have sorted our words alphabetically and now wish to order them by word length. To do this, we pass in a function object LessThan that compares two strings by length. Were we to use sort(), we would not be guaranteed to preserve the alphabetical ordering.

#include <algorithm> 
stable_sort( ia, ia+8 ); 
stable_sort( svec.begin(), svec.end(), greater<string>() ); 

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.