list::sort

具有的列表元素。有关升序或某些其他用户指定的顺序关系。

void sort( ); 
template<class Traits> 
   void sort(
      Traits _Comp
   );

参数

  • _Comp
    比较运算符用于排序顺序的元素。

备注

默认情况下第一个成员函数在升序将元素。

成员模板函数根据选件类 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;
}
  

要求

标头: <list>

命名空间: std

请参见

参考

list Class

标准模板库