List简介

  • list是一个双向链表容器,可高效地进行插入删除元素。
  • list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符。it++(ok), it+5(err)
  • #include <list>

1.list对象的默认构造

  • list采用采用模板类实现,对象的默认构造形式:list<T> lstT;  如:
  • list<int> lstInt;            //定义一个存放int的list容器。
  • list<float> lstFloat;     //定义一个存放float的list容器。
  • 尖括号内还可以设置指针类型或自定义类型。

2.list头尾的添加移除操作

  • list.push_back(elem);            //在容器尾部加入一个元素
  • list.pop_back();              //删除容器中最后一个元素
  • list.push_front(elem);     //在容器开头插入一个元素
  • list.pop_front();              //从容器开头移除第一个元素
  1. #include<iostream>
  2. using namespace std;
  3. #include <list>
  4. void objPlay2()
  5. {
  6. list<int> lstInt;
  7. lstInt.push_back();
  8. lstInt.push_back();
  9. lstInt.push_back();
  10. lstInt.push_back();
  11. lstInt.push_back();//这个时候list就是(1,3,5,7,9)
  12. lstInt.pop_front(); //删除头元素(1)这个时候list就是(3,5,7,9)
  13. lstInt.pop_front(); //删除头元素(3)这个时候list就是(5,7,9)
  14. lstInt.push_front();//头部插入11 这个时候list就是(11,5,7,9)
  15. lstInt.push_front();//头部插入13 这个时候list就是(13,11,5,7,9)
  16. lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,7)
  17. lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,)
  18. }
  19. int main()
  20. {
  21. objPlay2();
  22.  
  23. return ;
  24. }

3.list的数据存取

  • list.front();   //返回第一个元素。
  • list.back();  //返回最后一个元素。
  1. void objPlay3()
  2. {
  3. list<int> lstInt;
  4. lstInt.push_back();
  5. lstInt.push_back();
  6. lstInt.push_back();
  7. lstInt.push_back();
  8. lstInt.push_back();//这个时候list就是(1,3,5,7,9)
  9.  
  10. int iFront = lstInt.front(); //取到list的头部(1)
  11. int iBack = lstInt.back(); //取list的尾部(9)
  12. lstInt.front() = ; //给list的头部设置值,覆盖原来的值
  13. lstInt.back() = ; //给list的尾部设置值,覆盖原来的值
  14.  
  15. }

4.list与迭代器

  • list.begin();                     //返回容器中第一个元素的迭代器。
  • list.end();                       //返回容器中最后一个元素之后的迭代器。
  • list.rbegin();         //返回容器中倒数第一个元素的迭代器。
  • list.rend();         //返回容器中倒数最后一个元素的后面的迭代器。
  1. void objPlay4()
  2. {
  3. list<int> lstInt;
  4. lstInt.push_back();
  5. lstInt.push_back();
  6. lstInt.push_back();
  7. lstInt.push_back();
  8. lstInt.push_back();
  9.  
  10. for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); ++it)//正向迭代器
  11. {
  12. cout << *it << "\t";
  13. }
  14. cout << endl;
  15. for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); ++rit)//反向迭代器
  16. {
  17. cout << *rit << "\t";
  18. }
  19. }

5.list对象的带参数构造

  • list(beg,end);    //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。
  • list(n,elem);   //构造函数将n个elem拷贝给本身。
  • list(const list &lst);  //拷贝构造函数。
  1. void objPlay5()
  2. {
  3. list<int> lstIntA;
  4. lstIntA.push_back();
  5. lstIntA.push_back();
  6. lstIntA.push_back();
  7. lstIntA.push_back();
  8. lstIntA.push_back();
  9.  
  10. list<int> lstIntB(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
  11. list<int> lstIntC(, ); //8 8 8 8 8
  12. list<int> lstIntD(lstIntA); //1 3 5 7 9
  13.  
  14. }

6.list的赋值

  • list.assign(beg,end);    //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
  • list.assign(n,elem);  //将n个elem拷贝赋值给本身。
  • list& operator=(const list &lst);         //重载等号操作符
  • list.swap(lst);  // 将lst与本身的元素互换。
  1. void objPlay6()
  2. {
  3. list<int> lstIntA, lstIntB, lstIntC, lstIntD;//这个地方时先定义了list,注意和有参构造的区别
  4. lstIntA.push_back();
  5. lstIntA.push_back();
  6. lstIntA.push_back();
  7. lstIntA.push_back();
  8. lstIntA.push_back();
  9.  
  10. lstIntB.assign(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
  11. lstIntC.assign(, ); //8 8 8 8 8
  12. lstIntD = lstIntA; //1 3 5 7 9
  13. lstIntC.swap(lstIntD); //互换
  14.  
  15. }

7.list的大小

  • list.size();      //返回容器中元素的个数
  • list.empty();           //判断容器是否为空
  • list.resize(num);   //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
  • list.resize(num, elem);  //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
  1. void objPlay7()
  2. {
  3. list<int> lstIntA;
  4. lstIntA.push_back();
  5. lstIntA.push_back();
  6. lstIntA.push_back();
  7.  
  8. if (!lstIntA.empty())
  9. {
  10. int iSize = lstIntA.size(); //
  11. lstIntA.resize(); //1 3 5 0 0
  12. lstIntA.resize(, ); //1 3 5 0 0 1 1
  13. lstIntA.resize(); //1 3
  14. }
  15. }

8.list的插入

  • list.insert(pos,elem);   //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
  • list.insert(pos,n,elem);   //在pos位置插入n个elem数据,无返回值。
  • list.insert(pos,beg,end);   //在pos位置插入[beg,end)区间的数据,无返回值。
  1. void objPlay8()
  2. {
  3. list<int> lstA;
  4. list<int> lstB;
  5.  
  6. lstA.push_back();
  7. lstA.push_back();
  8. lstA.push_back();
  9. lstA.push_back();
  10. lstA.push_back();
  11.  
  12. lstB.push_back();
  13. lstB.push_back();
  14. lstB.push_back();
  15. lstB.push_back();
  16.  
  17. lstA.insert(lstA.begin(), ); //{11, 1, 3, 5, 7, 9}
  18. lstA.insert(++lstA.begin(), , ); //{11,33,33,1,3,5,7,9}
  19. lstA.insert(lstA.begin(), lstB.begin(), lstB.end()); //{2,4,6,8,11,33,33,1,3,5,7,9}
  20.  
  21. }

9.list的删除

  • list.clear();          //移除容器的所有数据
  • list.erase(beg,end);  //删除[beg,end)区间的数据,返回下一个数据的位置。注意是左闭右开
  • list.erase(pos);    //删除pos位置的数据,返回下一个数据的位置。
  • lst.remove(elem);   //删除容器中所有与elem值匹配的元素。
  1. void objPlay9()
  2. {
  3. //1.删除区间内的元素
  4. list<int> lstInt;
  5. lstInt.push_back();
  6. lstInt.push_back();
  7. lstInt.push_back();
  8. lstInt.push_back();
  9. lstInt.push_back();
  10. list<int>::iterator itBegin = lstInt.begin();
  11. ++itBegin;//begin指向3
  12. list<int>::iterator itEnd = lstInt.begin();
  13. ++itEnd;
  14. ++itEnd;
  15. ++itEnd;//end指向7
  16. lstInt.erase(itBegin, itEnd);
  17. //此时容器lstInt包含按顺序的1,7,9三个元素。
  18.  
  19. int arr[] = { , , , , , , , , , };
  20. for (list<int>::iterator it = lstInt.begin(); it != lstInt.end();) //小括号里不需写 ++it
  21. {
  22. if (*it == )
  23. {
  24. it = lstInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。//此时,不执行 ++it;
  25. }
  26. else
  27. {
  28. ++it;
  29. }
  30. }
  31.  
  32. //2.删除容器中等于3的元素的方法二
  33. lstInt.remove();
  34.  
  35. //3.删除lstInt的所有元素
  36. lstInt.clear(); //容器为空
  37.  
  38. }

10.list的反序排列

  • lst.reverse();     //反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
  1. void objPlay10()
  2. {
  3. list<int> lstInt;
  4.  
  5. lstInt.push_back();
  6. lstInt.push_back();
  7. lstInt.push_back();
  8. lstInt.push_back();
  9. lstInt.push_back();
  10.  
  11. lstInt.reverse(); //9 7 5 3 1
  12.  
  13. }

以上所有代码整理:

  1. #include<iostream>
  2. using namespace std;
  3. #include <list>
  4. void objPlay2()
  5. {
  6. list<int> lstInt;
  7. lstInt.push_back();
  8. lstInt.push_back();
  9. lstInt.push_back();
  10. lstInt.push_back();
  11. lstInt.push_back();//这个时候list就是(1,3,5,7,9)
  12. lstInt.pop_front(); //删除头元素(1)这个时候list就是(3,5,7,9)
  13. lstInt.pop_front(); //删除头元素(3)这个时候list就是(5,7,9)
  14. lstInt.push_front();//头部插入11 这个时候list就是(11,5,7,9)
  15. lstInt.push_front();//头部插入13 这个时候list就是(13,11,5,7,9)
  16. lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,7)
  17. lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,)
  18. }
  19. void objPlay3()
  20. {
  21. list<int> lstInt;
  22. lstInt.push_back();
  23. lstInt.push_back();
  24. lstInt.push_back();
  25. lstInt.push_back();
  26. lstInt.push_back();//这个时候list就是(1,3,5,7,9)
  27.  
  28. int iFront = lstInt.front(); //取到list的头部(1)
  29. int iBack = lstInt.back(); //取list的尾部(9)
  30. lstInt.front() = ; //给list的头部设置值,覆盖原来的值
  31. lstInt.back() = ; //给list的尾部设置值,覆盖原来的值
  32.  
  33. }
  34. void objPlay4()
  35. {
  36. list<int> lstInt;
  37. lstInt.push_back();
  38. lstInt.push_back();
  39. lstInt.push_back();
  40. lstInt.push_back();
  41. lstInt.push_back();
  42.  
  43. for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); ++it)//正向迭代器
  44. {
  45. cout << *it << "\t";
  46. }
  47. cout << endl;
  48. for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); ++rit)//反向迭代器
  49. {
  50. cout << *rit << "\t";
  51. }
  52. }
  53. void objPlay5()
  54. {
  55. list<int> lstIntA;
  56. lstIntA.push_back();
  57. lstIntA.push_back();
  58. lstIntA.push_back();
  59. lstIntA.push_back();
  60. lstIntA.push_back();
  61.  
  62. list<int> lstIntB(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
  63. list<int> lstIntC(, ); //8 8 8 8 8
  64. list<int> lstIntD(lstIntA); //1 3 5 7 9
  65.  
  66. }
  67. void objPlay6()
  68. {
  69. list<int> lstIntA, lstIntB, lstIntC, lstIntD;//这个地方时先定义了list,注意和有参构造的区别
  70. lstIntA.push_back();
  71. lstIntA.push_back();
  72. lstIntA.push_back();
  73. lstIntA.push_back();
  74. lstIntA.push_back();
  75.  
  76. lstIntB.assign(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
  77. lstIntC.assign(, ); //8 8 8 8 8
  78. lstIntD = lstIntA; //1 3 5 7 9
  79. lstIntC.swap(lstIntD); //互换
  80.  
  81. }
  82. void objPlay7()
  83. {
  84. list<int> lstIntA;
  85. lstIntA.push_back();
  86. lstIntA.push_back();
  87. lstIntA.push_back();
  88.  
  89. if (!lstIntA.empty())
  90. {
  91. int iSize = lstIntA.size(); //
  92. lstIntA.resize(); //1 3 5 0 0
  93. lstIntA.resize(, ); //1 3 5 0 0 1 1
  94. lstIntA.resize(); //1 3
  95. }
  96. }
  97. void objPlay8()
  98. {
  99. list<int> lstA;
  100. list<int> lstB;
  101.  
  102. lstA.push_back();
  103. lstA.push_back();
  104. lstA.push_back();
  105. lstA.push_back();
  106. lstA.push_back();
  107.  
  108. lstB.push_back();
  109. lstB.push_back();
  110. lstB.push_back();
  111. lstB.push_back();
  112.  
  113. lstA.insert(lstA.begin(), ); //{11, 1, 3, 5, 7, 9}
  114. lstA.insert(++lstA.begin(), , ); //{11,33,33,1,3,5,7,9}
  115. lstA.insert(lstA.begin(), lstB.begin(), lstB.end()); //{2,4,6,8,11,33,33,1,3,5,7,9}
  116.  
  117. }
  118. void objPlay9()
  119. {
  120. //1.删除区间内的元素
  121. list<int> lstInt;
  122. lstInt.push_back();
  123. lstInt.push_back();
  124. lstInt.push_back();
  125. lstInt.push_back();
  126. lstInt.push_back();
  127. list<int>::iterator itBegin = lstInt.begin();
  128. ++itBegin;//begin指向3
  129. list<int>::iterator itEnd = lstInt.begin();
  130. ++itEnd;
  131. ++itEnd;
  132. ++itEnd;//end指向7
  133. lstInt.erase(itBegin, itEnd);
  134. //此时容器lstInt包含按顺序的1,7,9三个元素。
  135.  
  136. int arr[] = { , , , , , , , , , };
  137. for (list<int>::iterator it = lstInt.begin(); it != lstInt.end();) //小括号里不需写 ++it
  138. {
  139. if (*it == )
  140. {
  141. it = lstInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。//此时,不执行 ++it;
  142. }
  143. else
  144. {
  145. ++it;
  146. }
  147. }
  148.  
  149. //2.删除容器中等于3的元素的方法二
  150. lstInt.remove();
  151.  
  152. //3.删除lstInt的所有元素
  153. lstInt.clear(); //容器为空
  154.  
  155. }
  156. void objPlay10()
  157. {
  158. list<int> lstInt;
  159.  
  160. lstInt.push_back();
  161. lstInt.push_back();
  162. lstInt.push_back();
  163. lstInt.push_back();
  164. lstInt.push_back();
  165.  
  166. lstInt.reverse(); //9 7 5 3 1
  167.  
  168. }
  169.  
  170. int main()
  171. {
  172. objPlay2();
  173. objPlay3();
  174. objPlay4();
  175. objPlay5();
  176. objPlay6();
  177. objPlay7();
  178. objPlay8();
  179. objPlay9();
  180. objPlay10();
  181. system("pause");
  182. return ;
  183. }

STL学习系列六:List容器的更多相关文章

  1. 标准模板库(STL)学习探究之vector容器

    标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...

  2. 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)

    layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...

  3. STL学习系列之一——标准模板库STL介绍

    库是一系列程序组件的集合,他们可以在不同的程序中重复使用.C++语言按照传统的习惯,提供了由各种各样的函数组成的库,用于完成诸如输入/输出.数学计算等功能. 1. STL介绍 标准模板库STL是当今每 ...

  4. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  5. STL学习系列三:Deque容器

    1.Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口上和vector非常 ...

  6. STL学习系列二:Vector容器

    1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...

  7. STL学习系列四:Stack容器

    Stack简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. #include <stack> 1.stack对象的默认构造 ...

  8. STL学习系列九:Map和multimap容器

    1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...

  9. STL学习系列八:Set和multiset容器

    1.set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实 ...

随机推荐

  1. tornado中使用torndb,连接数过高的问题

    问题背景 最近新的产品开发中,使用了到了Tornado和mysql数据库.但在基本框架完成之后,我在开发时候发现了一个很奇怪的现象,我在测试时,发现数据库返回不了结果,于是我在mysql中输入show ...

  2. IT玄幻小说

    职业 设计:菜鸟  美工<初级,中级,高级,资深>  设计师<初级,中级,高级,资深>  大神 前端:菜鸟  前端<初级,中级,高级,资深> 架构师<初级,中 ...

  3. 事务报错 [Exception] 当前 TransactionScope 已完成

    捕获异常的时候 偶尔会碰到这个异常报告 导致原因 //正确代码 using (TransactionScope ts = new TransactionScope(TransactionScopeOp ...

  4. [总结]FFMPEG视音频编解码零基础学习方法--转

    ffmpeg编解码学习   目录(?)[-] ffmpeg程序的使用ffmpegexeffplayexeffprobeexe 1 ffmpegexe 2 ffplayexe 3 ffprobeexe ...

  5. linux lnmp编译安装

    关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled ...

  6. liunx上运行mybase

    mybase 是个人知识管理软件,国内用他的人很多,尤其是程序员.我也是mybase的忠实使用者,有大量的积累.    以前一直用Windows,mybase使用完全没有问题,后来转投ubuntu阵营 ...

  7. Wireshark和TcpDump抓包分析心得

    Wireshark和 TcpDump抓包分析心得  1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Window ...

  8. .NET Remoting

    .NET Remoting   .NET Remoting是微软早期的分布式通信技术,虽然微软后来通过WCF通用基础通信框架整合掉了,但是通过回顾学习Remoting,反过来学习理解WCF也是很有帮助 ...

  9. spoj 694(后缀数组)

    题意:求一个字符串的不重复子串的个数. 分析:对于下标为i的位置,能够产生的前缀子串个数为len-i(下标从0开始),对于与它字典序相邻的后缀产生的子串是重复的(就是他们的最长公共前缀),所以我们要减 ...

  10. A标记点击后去掉虚线

    没加时,谷歌无虚线,但ie有虚线. 加上这句,所有的a标签点击都没有虚线了. a {outline: none;} a:active {star:expression_r(this.onFocus=t ...