c++List用法

assign() 给list赋值 
back() 返回最后一个元素 
begin() 返回指向第一个元素的迭代器 
clear() 删除所有元素 
empty() 如果list是空的则返回true 
end() 返回末尾的迭代器 
erase() 删除一个元素 
front() 返回第一个元素 
get_allocator() 返回list的配置器 
insert() 插入一个元素到list中 
max_size() 返回list能容纳的最大元素数量 
merge() 合并两个list 
pop_back() 删除最后一个元素 
pop_front() 删除第一个元素 
push_back() 在list的末尾添加一个元素 
push_front() 在list的头部添加一个元素 
rbegin() 返回指向第一个元素的逆向迭代器 
remove() 从list删除元素 
remove_if() 按指定条件删除元素 
rend() 指向list末尾的逆向迭代器 
resize() 改变list的大小 
reverse() 把list的元素倒转 
size() 返回list中的元素个数 
sort() 给list排序 
splice() 合并两个list 
swap() 交换两个list 
unique() 删除list中重复的元素


实例一:

[cpp]  view plain  copy
  1. #include <iostream>   
  2. #include <list>   
  3. #include <numeric>   
  4. #include <algorithm>   
  5. using namespace std;   
  6.   
  7. //创建一个list容器的实例LISTINT   
  8. typedef list<int> LISTINT;   
  9. //创建一个list容器的实例LISTCHAR   
  10. typedef list<char> LISTCHAR;   
  11.   
  12. void main()   
  13. {   
  14.     //用list容器处理整型数据    
  15.     //用LISTINT创建一个名为listOne的list对象   
  16.     LISTINT listOne;   
  17.     //声明i为迭代器   
  18.     LISTINT::iterator i;   
  19.       
  20.     //从前面向listOne容器中添加数据   
  21.     listOne.push_front (2);   
  22.     listOne.push_front (1);   
  23.       
  24.     //从后面向listOne容器中添加数据   
  25.     listOne.push_back (3);   
  26.     listOne.push_back (4);   
  27.       
  28.     //从前向后显示listOne中的数据   
  29.     cout<<"listOne.begin()--- listOne.end():"<<endl;   
  30.     for (i = listOne.begin(); i != listOne.end(); ++i)   
  31.         cout << *i << " ";   
  32.     cout << endl;   
  33.       
  34.     //从后向后显示listOne中的数据   
  35.     LISTINT::reverse_iterator ir;   
  36.     cout<<"listOne.rbegin()---listOne.rend():"<<endl;   
  37.     for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {   
  38.         cout << *ir << " ";   
  39.     }   
  40.     cout << endl;   
  41.       
  42.     //使用STL的accumulate(累加)算法   
  43.     int result = accumulate(listOne.begin(), listOne.end(),0);   
  44.     cout<<"Sum="<<result<<endl;   
  45.     cout<<"------------------"<<endl;   
  46.       
  47.     //--------------------------   
  48.     //用list容器处理字符型数据   
  49.     //--------------------------   
  50.       
  51.     //用LISTCHAR创建一个名为listOne的list对象   
  52.     LISTCHAR listTwo;   
  53.     //声明j为迭代器   
  54.     LISTCHAR::iterator j;   
  55.       
  56.     //从前面向listTwo容器中添加数据   
  57.     listTwo.push_front ('A');   
  58.     listTwo.push_front ('B');   
  59.       
  60.     //从后面向listTwo容器中添加数据   
  61.     listTwo.push_back ('x');   
  62.     listTwo.push_back ('y');   
  63.       
  64.     //从前向后显示listTwo中的数据   
  65.     cout<<"listTwo.begin()---listTwo.end():"<<endl;   
  66.     for (j = listTwo.begin(); j != listTwo.end(); ++j)   
  67.         cout << char(*j) << " ";   
  68.     cout << endl;   
  69.       
  70.     //使用STL的max_element算法求listTwo中的最大元素并显示   
  71.     j=max_element(listTwo.begin(),listTwo.end());   
  72.     cout << "The maximum element in listTwo is: "<<char(*j)<<endl;   
  73. }

  74.    
  75. typedef list<int> LISTINT;  
    int main(void) { 
     int a[5] = {1,5,3,5,6};  LISTINT ls1; 
     ls1.assign(a,a+5);  LISTINT::iterator it; 
     for( it=ls1.begin(); it!=ls1.end(); it++)   cout<<*it<<" ";  cout<<endl; 
    //输出 1 5 3 5 6 
     ls1.insert( ls1.end(), 4 ); 
     for( it=ls1.begin(); it!=ls1.end(); it++)   cout<<*it<<" ";  cout<<endl; 
    //输出 1 5 3 5 6 4  ls1.remove( 5 ); 
     for( it=ls1.begin(); it!=ls1.end(); it++)   cout<<*it<<" ";  cout<<endl; } 
    //输出 1 3 6 4 【5元素全部被删除了】
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值