List双向链表

再谈链表

List链表的概念再度出现了,作为线性表的一员,C++的STL提供了快速进行构建的方法,为此,在前文的基础上通过STL进行直接使用,这对于程序设计中快速构建原型是相当有必要的,这里的STL链表是单链表的形式。

头文件

头文件:#include<list>

初始化

格式为:explicit list (const allocator_type& alloc = allocator_type());

我们以int类型作为参数为例进行创建,其创建方法与vector无异

定义的代码如下:

  1. list<int> l1; //创建一个空链表
  2. list<int> l2(10); //创建一个链表其有10个空元素
  3. list<int> l3(5,20); //创建一个链表其有5个元素内容为20
  4. list<int> l4(l3.begin(),l3.end()); //创建一个链表其内容为l3的内容
  5. list<int> l5(l4); //创建一个链表其内容为l4的内容

除此之外,还可以直接使用数组来初始化向量:

  1. int n[] = { 1, 2, 3, 4, 5 };
  2. list<int> a(n, n + 5); // 将数组n的前5个元素作为列表a的初值

迭代器

遍历代码举例(其方法和vector版本无异只是更加精简):

  1. list<int> li;
  2. for(list<int>::iterator it=li.begin();it!=li.end();it++){
  3. cout<<*it<<' ';
  4. }

基本操作

3.1 容量函数

  • 容器大小:lst.size();
  • 容器最大容量:lst.max_size();
  • 更改容器大小:lst.resize();
  • 容器判空:lst.empty();
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. list<int> lst;
  9. for (int i = 0; i<6; i++)
  10. {
  11. lst.push_back(i);
  12. }
  13.  
  14. cout << lst.size() << endl; // 输出:6
  15. cout << lst.max_size() << endl; // 输出:357913941
  16. lst.resize(0); // 更改元素大小
  17. cout << lst.size() << endl; // 输出:0
  18. if (lst.empty())
  19. cout << "元素为空" << endl; // 输出:元素为空
  20.  
  21. return 0;
  22. }

3.2 添加函数

  • 头部添加元素:lst.push_front(const T& x);
  • 末尾添加元素:lst.push_back(const T& x);
  • 任意位置插入一个元素:lst.insert(iterator it, const T& x);
  • 任意位置插入 n 个相同元素:lst.insert(iterator it, int n, const T& x);
  • 插入另一个向量的 [forst,last] 间的数据:lst.insert(iterator it, iterator first, iterator last);
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. list<int> lst;
  9.  
  10. // 头部增加元素
  11. lst.push_front(4);
  12. // 末尾添加元素
  13. lst.push_back(5);
  14. // 任意位置插入一个元素
  15. list<int>::iterator it = lst.begin();
  16. lst.insert(it, 2);
  17. // 任意位置插入n个相同元素
  18. lst.insert(lst.begin(), 3, 9);
  19. // 插入另一个向量的[forst,last]间的数据
  20. list<int> lst2(5, 8);
  21. lst.insert(lst.begin(), lst2.begin(), ++lst2.begin());
  22.  
  23. // 遍历显示
  24. for (it = lst.begin(); it != lst.end(); it++)
  25. cout << *it << " "; // 输出:8 9 9 9 2 4 5
  26. cout << endl;
  27.  
  28. return 0;
  29. }
  1. li.insert(li.begin(),10); //在链表最前端插入数据10
  2. li.insert(li.begin(),5,20); //在链表最前端插入5个数据内容为20
  3.  
  4. list<int> k(2,50); //创建一个新的链表k,其拥有2个元素内容均为50
  5. li.insert(li.begin(),li.begin(),li.end()); //在链表v最前端插入链表上K的全部内容

3.3 删除函数

  • 头部删除元素:lst.pop_front();
  • 末尾删除元素:lst.pop_back();
  • 任意位置删除一个元素:lst.erase(iterator it);
  • 删除 [first,last] 之间的元素:lst.erase(iterator first, iterator last);
  • 清空所有元素:lst.clear();
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. list<int> lst;
  9. for (int i = 0; i < 8; i++)
  10. lst.push_back(i);
  11.  
  12. // 头部删除元素
  13. lst.pop_front();
  14. // 末尾删除元素
  15. lst.pop_back();
  16. // 任意位置删除一个元素
  17. list<int>::iterator it = lst.begin();
  18. lst.erase(it);
  19. // 删除[first,last]之间的元素
  20. lst.erase(lst.begin(), ++lst.begin());
  21.  
  22. // 遍历显示
  23. for (it = lst.begin(); it != lst.end(); it++)
  24. cout << *it << " "; // 输出:3 4 5 6
  25. cout << endl;
  26.  
  27. // 清空所有元素
  28. lst.clear();
  29.  
  30. // 判断list是否为空
  31. if (lst.empty())
  32. cout << "元素为空" << endl; // 输出:元素为空
  33.  
  34. return 0;
  35. }
  1. li.erase(li.begin()); //删除第一个元素
  2. li.erase(li.begin(),li.begin()+4); //删除前4个元素

3.4 访问函数

  • 访问第一个元素:lst.front();
  • 访问最后一个元素:lst.back();
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. list<int> lst;
  9. for (int i = 0; i < 6; i++)
  10. lst.push_back(i);
  11.  
  12. // 访问第一个元素
  13. cout << lst.front() << endl; // 输出:0
  14. // 访问最后一个元素
  15. cout << lst.back() << endl; // 输出:5
  16.  
  17. return 0;
  18. }

3.5 其他函数

  • 多个元素赋值:lst.assign(int nSize, const T& x); // 类似于初始化时用数组进行赋值
  • 交换两个同类型容器的元素:swap(list&, list&); 或 lst.swap(list&);
  • 合并两个列表的元素(默认升序排列):lst.merge();
  • 在任意位置拼接入另一个list:lst.splice(iterator it, list&);
  • 删除容器中相邻的重复元素:lst.unique();
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. // 多个元素赋值s
  9. list<int> lst1;
  10. lst1.assign(3, 1);
  11. list<int> lst2;
  12. lst2.assign(3, 2);
  13.  
  14. // 交换两个容器的元素
  15. // swap(lst1, lst2); // ok
  16. lst1.swap(lst2);
  17. // 遍历显示
  18. cout << "交换后的lst1: ";
  19. list<int>::iterator it;
  20. for (it = lst1.begin(); it!=lst1.end(); it++)
  21. cout << *it << " "; // 输出:2 2 2
  22. cout << endl;
  23.  
  24. // 遍历显示
  25. cout << "交换后的lst2: ";
  26. for (it = lst2.begin(); it != lst2.end(); it++)
  27. cout << *it << " "; // 输出:1 1 1
  28. cout << endl;
  29.  
  30. list<int> lst3;
  31. lst3.assign(3, 3);
  32. list<int> lst4;
  33. lst4.assign(3, 4);
  34. // 合并两个列表的元素
  35. lst4.merge(lst3); // 不是简单的拼接,而是会升序排列
  36. cout << "合并后的lst4: ";
  37. for (it = lst4.begin(); it != lst4.end(); it++)
  38. cout << *it << " "; // 输出:3 3 3 4 4 4
  39. cout << endl;
  40.  
  41. list<int> lst5;
  42. lst5.assign(3, 5);
  43. list<int> lst6;
  44. lst6.assign(3, 6);
  45. // 在lst6的第2个元素处,拼接入lst5
  46. lst6.splice(++lst6.begin(), lst5);
  47. cout << "拼接后的lst6: ";
  48. for (it = lst6.begin(); it != lst6.end(); it++)
  49. cout << *it << " "; // 输出:6 5 5 5 6 6
  50. cout << endl;
  51.  
  52. // 删除容器中相邻的重复元素
  53. list<int> lst7;
  54. lst7.push_back(1);
  55. lst7.push_back(1);
  56. lst7.push_back(2);
  57. lst7.push_back(2);
  58. lst7.push_back(3);
  59. lst7.push_back(2);
  60. lst7.unique();
  61. cout << "删除容器中相邻的重复元素后的lst7: ";
  62. for (it = lst7.begin(); it != lst7.end(); it++)
  63. cout << *it << " "; // 输出:1 2 3 2
  64. cout << endl;
  65.  
  66. return 0;
  67. }

排序sort()

  1. #include<iostream>
  2. #include<list>
  3. using namespace std;s
  4. int cmp(const int &a,const int &b){
  5. //简单的自定义降序序列
  6. return a>b;
  7. }
  8. int main(){
  9. list<int> li; //创建一个空链表
  10. for(int i=10;i>=6;i--){
  11. li.push_back(i);
  12. }
  13. li.push_front(3);
  14. li.push_back(20);
  15. list<int> li2(li);
  16. for(list<int>::iterator it=li.begin();it!=li.end();it++){
  17. cout<<*it<<' ';
  18. }
  19. cout<<endl;
  20. //排序前3 10 9 8 7 6 20//
  21. li.sort();
  22.  
  23. for(list<int>::iterator it=li.begin();it!=li.end();it++){
  24. cout<<*it<<' ';
  25. }
  26. cout<<endl;
  27. //默认排序后 3 6 7 8 9 10 20//
  28. li2.sort(cmp);
  29. for(list<int>::iterator it=li2.begin();it!=li2.end();it++){
  30. cout<<*it<<' ';
  31. }
  32. cout<<endl;
  33. //自定义排序后 20 10 9 8 7 6 3//
  34. return 0;
  35. }

迭代器与算法

1. 迭代器

  • 开始迭代器指针:lst.begin();
  • 末尾迭代器指针:lst.end(); // 指向最后一个元素的下一个位置
  • 指向常量的开始迭代器指针:lst.cbegin(); // 意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。
  • 指向常量的末尾迭代器指针:lst.cend();
  • 反向迭代器指针,指向最后一个元素:lst.rbegin();
  • 反向迭代器指针,指向第一个元素的前一个元素:lst.rend();
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. list<int> lst;
  9. lst.push_back(1);
  10. lst.push_back(2);
  11. lst.push_back(3);
  12.  
  13. cout << *(lst.begin()) << endl; // 输出:1
  14. cout << *(--lst.end()) << endl; // 输出:3
  15. cout << *(lst.cbegin()) << endl; // 输出:1
  16. cout << *(--lst.cend()) << endl; // 输出:3
  17. cout << *(lst.rbegin()) << endl; // 输出:3
  18. cout << *(--lst.rend()) << endl; // 输出:1
  19. cout << endl;
  20.  
  21. return 0;
  22. }

2. 算法

  • 遍历元素
  1. list<int>::iterator it;
  2. for (it = lst.begin(); it != lst.end(); it++)
  3. cout << *it << endl;
  • 元素翻转
  1. #include <algorithm>
  2. reverse(lst.begin(), lst.end());
  • 元素排序
  1. #include <algorithm>
  2. sort(lst.begin(), lst.end()); // 采用的是从小到大的排序
  3.  
  4. // 如果想从大到小排序,可以采用先排序后反转的方式,也可以采用下面方法:
  5. // 自定义从大到小的比较器,用来改变排序方式
  6. bool Comp(const int& a, const int& b)
  7. {
  8. return a > b;
  9. }
  10.  
  11. sort(lst.begin(), lst.end(), Comp);

总结

可以看到,list 与 vector、deque 的用法基本一致,除了以下几处不同:

  • list 为双向迭代器,故不支持it+=i
  • list 不支持下标访问和at方法访问。

list使用详解的更多相关文章

  1. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  2. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  3. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  4. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  5. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  6. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  7. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  8. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  9. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

  10. .NET应用和AEAI CAS集成详解

    1 概述 数通畅联某综合SOA集成项目的统一身份认证工作,需要第三方系统配合进行单点登录的配置改造,在项目中有需要进行单点登录配置的.NET应用系统,本文专门记录.NET应用和AEAI CAS的集成过 ...

随机推荐

  1. 在STM32F401上移植uC/OS的一个小问题 [原创]

    STM32F401xx是意法半导体新推出的Cortex-M4内核的MCU,相较于已经非常流行的STM32F407xx和STM32F427xx等相同内核的MCU而言,其特点是功耗仅为128uA/MHz, ...

  2. SQL 练习16

    按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SELECT * from SC LEFT JOIN (SELECT sid,AVG(score) 平均成绩 from SC GROUP B ...

  3. 在docker安装tomcat的时候,报错:Caused by: java.lang.IllegalArgumentException: The AJP Connector is configured with secretRequired="true

    初识docker,试着在docker中安装tomcat(安装的tomcat8.5),并且挂载到宿主机的相关目录下,结果启动的时候报错: 12-May-2020 01:14:34.061 SEVERE ...

  4. 栈编程和函数控制流: 从 continuation 与 CPS 讲到 call/cc 与协程

    原标题:尾递归优化 快速排序优化 CPS 变换 call/cc setjmp/longjmp coroutine 协程 栈编程和控制流 讲解 本文为部分函数式编程的扩展及最近接触编程语言控制流的学习和 ...

  5. DataTable 读取每一行的内容

    foreach (DataRow item in dataTable.Rows) { for (int i = 0; i < dataTable.Columns.Count; i++) { Co ...

  6. asp.net core 声明controller的方法

    1, 对类名直接添加Controller, 如TestController. 2,  继承Controller 类. 3, 对类名添加controller的属性, 如[Controller]

  7. 【spring 注解驱动开发】Spring AOP原理

    尚学堂spring 注解驱动开发学习笔记之 - AOP原理 AOP原理: 1.AOP原理-AOP功能实现 2.AOP原理-@EnableAspectJAutoProxy 3.AOP原理-Annotat ...

  8. .NET 元数据概述

    元数据是一种二进制信息,用以对存储在公共语言运行库可移植可执行文件 (PE) 文件或存储在内存中的程序进行描述.将您的代码编译为 PE 文件时,便会将元数据插入到该文件的一部分中,而将代码转换为 Mi ...

  9. QT中的对象模型――QPointer

    QPointer是一个模板类,为QObject对象提供了守卫指针(Guarded Pointer).什么是守卫指针?守卫指针QPointer<T>类似于普通C++指针T *,有且仅有一点不 ...

  10. 关于Ubuntu18.04 linux系统使用安装JDK Mysql

    平台部署 一.安装JDK step1.下载OracleJDKstep2. 解压step3. 加入环境变量 具体操作如下: lemon@ubuntu:~$ cd ~/download/ lemon@ub ...