序列容器(list)

list序列容器

List序列容器提供了在容器的任何位置执行高效的插入和删除功能。

类模板list是用双链表实现的。也就是说,链表中每个节点都包含指向链表中前一个节点的一个指针和指向链表中后一个节点的指针。这就使得类模板list支持双向迭代器,允许以向前和向后的方式遍历容器。

下面以demo中的例子来说明list的用法。

[cpp]  view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4. #include <list>  
  5. #include <algorithm>  
  6. #include <iterator>  
  7.   
  8. template<typename T> void printList(const list<T> &listRef);  
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.  const int SIZE = 4;  
  12.  int array[SIZE] = {2,6,4,8};  
  13.  std::list<int> values;  
  14.  std::list<int> otherValues;  
  15.   
  16.  //push_front在values开始出插入元素,push_back在values的末尾处插入元素  
  17.  values.push_front(1);  
  18.  values.push_front(2);  
  19.  values.push_back(3);  
  20.  values.push_back(4);  
  21.   
  22.  cout<<"元素值为:";  
  23.  printList(values);  
  24.   
  25.  //sort成员函数按照升序排列list中的元素  
  26.  values.sort();   
  27.  cout<<"\n排序后包含的元素为:";  
  28.  printList(values);  
  29.    
  30.  //在otherValues中插入元素  
  31.  otherValues.insert(otherValues.begin(),array,array+SIZE);  
  32.  cout<<"\n插入后,otherValues包含:";  
  33.  printList(otherValues);  
  34.   
  35.  //删除otherValues中的元素,并插入到values中由第一个实参指定的迭代器前  
  36.  values.splice(values.end(),otherValues);  
  37.  cout<<"\nsplices 后,values包含:";  
  38.  printList(values);  
  39.   
  40.  values.sort();  
  41.  cout<<"\n排序后,values包含的元素为:";  
  42.  printList(values);  
  43.   
  44.  otherValues.insert(otherValues.begin(),array,array+SIZE);  
  45.  otherValues.sort();  
  46.  cout<<"\n插入并排序后,otherValues包含:";  
  47.  printList(otherValues);  
  48.   
  49.  //移除otherValues元素并按照顺序插入到values中  
  50.  values.merge(otherValues);  
  51.  cout<<"\n合并后:\n values包含:";  
  52.  printList(values);  
  53.  cout<<"\n otherValues 包含:";  
  54.  printList(otherValues);  
  55.   
  56.  values.pop_front();//删除list中的第一个元素  
  57.  values.pop_back();//删除list中的最后第一个元素  
  58.  cout<<"\npop_front和pop_back后:\n values包含:";  
  59.  printList(values);  
  60.   
  61.  values.unique();//删除list中的所有重复元素,在执行这个操作前list必须是已经排好序的  
  62.  cout<<"\nunique后,values包含:";  
  63.  printList(values);  
  64.   
  65.  //交换values和otherValues的元素  
  66.  values.swap(otherValues);  
  67.  cout<<"\nswap后:\n values包含:";  
  68.  printList(values);  
  69.  cout<<"\n otherValues包含:";  
  70.  printList(otherValues);  
  71.   
  72.  //用otherValues中两个迭代器实参制定范围的内容替换values的内容  
  73.  values.assign(otherValues.begin(),otherValues.end());  
  74.  cout<<"\nassign后,values包含:";  
  75.  printList(values);  
  76.   
  77.  values.merge(otherValues);  
  78.  cout<<"\nmerge后,values包含:";  
  79.  printList(values);  
  80.   
  81.  values.remove(4);//从list中删除值4的所有副本  
  82.  cout<<"\nremove(4)后,values包含:";  
  83.  printList(values);  
  84.  cout<<endl;  
  85.  system("pause");  
  86.  return 0;  
  87. }  
  88.   
  89. template<typename T> void printList(const list<T> &listRef)  
  90. {  
  91.  if(listRef.empty())  
  92.  {  
  93.   cout<<"list is empty";  
  94.  }  
  95.  else  
  96.  {  
  97.   ostream_iterator<T> output(cout," ");  
  98.   copy(listRef.begin(),listRef.end(),output);  
  99.  }  
  100. }  


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值