使用c++中的list排序


#include <list>
#include <iostream>
using namespace std;

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

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

   cout << "输入需要排序的个数:" ;
   cin >> num; 
  
   for(int i=0;i<num;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<int>( ) );
   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_typeA type that represents the allocator class for a list object.
const_iteratorA type that provides a bidirectional iterator that can read a const element in a list.
const_pointerA type that provides a pointer to a const element in a list.
const_referenceA type that provides a reference to a const element stored in a list for reading and performing const operations.
const_reverse_iteratorA type that provides a bidirectional iterator that can read any const element in a list.
difference_typeA type that provides the difference between two iterators that refer to elements within the same list.
iteratorA type that provides a bidirectional iterator that can read or modify any element in a list.
pointerA type that provides a pointer to an element in a list.
referenceA type that provides a reference to a const element stored in a list for reading and performing const operations.
reverse_iteratorA type that provides a bidirectional iterator that can read or modify an element in a reversed list.
size_typeA type that counts the number of elements in a list.
value_typeA type that represents the data type stored in a list.

Member Functions

assignErases elements from a list and copies a new set of elements to the target list.
backReturns a reference to the last element of a list.
beginReturns an iterator addressing the first element in a list.
clearErases all the elements of a list.
emptyTests if a list is empty.
endReturns an iterator that addresses the location succeeding the last element in a list.
eraseRemoves an element or a range of elements in a list from specified positions.
frontReturns a reference to the first element in a list.
get_allocatorReturns a copy of the allocator object used to construct a list.
insertInserts an element or a number of elements or a range of elements into a list at a specified position.
listConstructs 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_sizeReturns the maximum length of a list.
mergeRemoves 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_backDeletes the element at the end of a list.
pop_frontDeletes the element at the beginning of a list.
push_backAdds an element to the end of a list.
push_frontAdds an element to the beginning of a list.
rbeginReturns an iterator addressing the first element in a reversed list.
removeErases elements in a list that match a specified value.
remove_ifErases elements from the list for which a specified predicate is satisfied.
rendReturns an iterator that addresses the location succeeding the last element in a reversed list.
resizeSpecifies a new size for a list.
reverseReverses the order in which the elements occur in a list.
sizeReturns the number of elements in a list.
sortArranges the elements of a list in ascending order or with respect to some other order relation.
spliceRemoves elements from the argument list and inserts them into the target list.
swapExchanges the elements of two lists.
uniqueRemoves adjacent duplicate elements or adjacent elements that satisfy some other binary predicate from the list.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值