使用c++中的list排序


#include
#include
using namespace std;

int main( )
{
   list c1;
   list ::iterator c1_Iter;

   int num,var;//for input num and var for the list

   cout << "输入需要排序的个数:" ;
   cin >> num; 
  
   for(int i=0;i   {
    var = 0;
    cout << "输入第" << i+1 << "个数:";
    cin >> var;
   
    c1.push_back(var);
   }

   cout << "排序前:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   c1.sort( );
   cout << "排序后:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   c1.sort( greater( ) );
   cout << "按照降序重新排序是:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

 //  cin >> num;  //留住屏幕结果
   return 0;

另外把MSDN上的list的memebers也贴上参考

list Members

Typedefs

allocator_type A type that represents the allocator class for a list object.
const_iterator A type that provides a bidirectional iterator that can read a const element in a list.
const_pointer A type that provides a pointer to a const element in a list.
const_reference A type that provides a reference to a const element stored in a list for reading and performing const operations.
const_reverse_iterator A type that provides a bidirectional iterator that can read any const element in a list.
difference_type A type that provides the difference between two iterators that refer to elements within the same list.
iterator A type that provides a bidirectional iterator that can read or modify any element in a list.
pointer A type that provides a pointer to an element in a list.
reference A type that provides a reference to a const element stored in a list for reading and performing const operations.
reverse_iterator A type that provides a bidirectional iterator that can read or modify an element in a reversed list.
size_type A type that counts the number of elements in a list.
value_type A type that represents the data type stored in a list.

Member Functions

assign Erases elements from a list and copies a new set of elements to the target list.
back Returns a reference to the last element of a list.
begin Returns an iterator addressing the first element in a list.
clear Erases all the elements of a list.
empty Tests if a list is empty.
end Returns an iterator that addresses the location succeeding the last element in a list.
erase Removes an element or a range of elements in a list from specified positions.
front Returns a reference to the first element in a list.
get_allocator Returns a copy of the allocator object used to construct a list.
insert Inserts an element or a number of elements or a range of elements into a list at a specified position.
list Constructs a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other list.
max_size Returns the maximum length of a list.
merge Removes the elements from the argument list, inserts them into the target list, and orders the new, combined set of elements in ascending order or in some other specified order.
pop_back Deletes the element at the end of a list.
pop_front Deletes the element at the beginning of a list.
push_back Adds an element to the end of a list.
push_front Adds an element to the beginning of a list.
rbegin Returns an iterator addressing the first element in a reversed list.
remove Erases elements in a list that match a specified value.
remove_if Erases elements from the list for which a specified predicate is satisfied.
rend Returns an iterator that addresses the location succeeding the last element in a reversed list.
resize Specifies a new size for a list.
reverse Reverses the order in which the elements occur in a list.
size Returns the number of elements in a list.
sort Arranges the elements of a list in ascending order or with respect to some other order relation.
splice Removes elements from the argument list and inserts them into the target list.
swap Exchanges the elements of two lists.
unique Removes adjacent duplicate elements or adjacent elements that satisfy some other binary predicate from the list.

你可能感兴趣的:(使用c++中的list排序)