list::sort
Visual Studio 2012
Arranges the elements of a list in ascending order or with respect to some other user-specified order relation.
void sort( );
template<class Traits>
void sort(
Traits _Comp
);
// list_sort.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
c1.push_back( 20 );
c1.push_back( 10 );
c1.push_back( 30 );
cout << "Before sorting: c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
c1.sort( );
cout << "After sorting c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
c1.sort( greater<int>( ) );
cout << "After sorting with 'greater than' operation, c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
}
Before sorting: c1 = 20 10 30 After sorting c1 = 10 20 30 After sorting with 'greater than' operation, c1 = 30 20 10