当前位置:   article > 正文

【C++ STL链表:节点串联,数据无限,解锁高效插入、删除与迭代的奇妙能力】

【C++ STL链表:节点串联,数据无限,解锁高效插入、删除与迭代的奇妙能力】

【本节目标】

  • 1. list的介绍及使用

  • 2. list的深度剖析及模拟实现

  • 3. list与vector的对比

1. list的介绍及使用

1.1 list的介绍

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高 效。

4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率 更好。

5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这 可能是一个重要的因素)

1.2 list的使用

list中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已达到可扩展 的能力。以下为list中一些常见的重要接口。

1.2.1 list的构造

构造函数( (constructor)) 接口说明
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素
list() 构造空的list
list (const list& x) 拷贝构造函数
list (InputIterator first, InputIterator last)用[first, last)区间中的元素构造list
  1. // list的构造
  2. void TestList1()
  3. {
  4. list<int> l1; // 构造空的l1
  5. list<int> l2(4, 100); // l2中放4个值为100的元素
  6. list<int> l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
  7. list<int> l4(l3); // 用l3拷贝构造l4
  8. // 以数组为迭代器区间构造l5
  9. int array[] = { 16,2,77,29 };
  10. list<int> l5(array, array + sizeof(array) / sizeof(int));
  11. // 列表格式初始化C++11
  12. list<int> l6{ 1,2,3,4,5 };
  13. // 用迭代器方式打印l5中的元素
  14. list<int>::iterator it = l5.begin();
  15. while (it != l5.end())
  16. {
  17. cout << *it << " ";
  18. ++it;
  19. }
  20. cout << endl;
  21. // C++11范围for的方式遍历
  22. for (auto& e : l5)
  23. cout << e << " ";
  24. cout << endl;
  25. }

1.2.2 list iterator的使用

此处,可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。

函数声明 接口说明
begin + end 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin + rend返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置

【注意】

  • 1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
  • 2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
  1. // list迭代器的使用
  2. // 注意:遍历链表只能用迭代器和范围for
  3. void PrintList(const list<int>& l)
  4. {
  5. // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
  6. for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
  7. {
  8. cout << *it << " ";
  9. // *it = 10; 编译不通过
  10. }
  11. cout << endl;
  12. }
  13. void TestList2()
  14. {
  15. int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  16. list<int> l(array, array + sizeof(array) / sizeof(array[0]));
  17. // 使用正向迭代器正向list中的元素
  18. // list<int>::iterator it = l.begin(); // C++98中语法
  19. auto it = l.begin(); // C++11之后推荐写法
  20. while (it != l.end())
  21. {
  22. cout << *it << " ";
  23. ++it;
  24. }
  25. cout << endl;
  26. // 使用反向迭代器逆向打印list中的元素
  27. // list<int>::reverse_iterator rit = l.rbegin();
  28. auto rit = l.rbegin();
  29. while (rit != l.rend())
  30. {
  31. cout << *rit << " ";
  32. ++rit;
  33. }
  34. cout << endl;
  35. }

1.2.3 list capacity

函数声明接口说明
empty 检测list是否为空,是返回true,否则返回false
size返回list中有效节点的个数

1.2.4 list element access

函数声明 接口说明
front 返回list的第一个节点中值的引用
back 返回list的最后一个节点中值的引用

1.2.5 list modifiers

函数声明 接口说明
push_front在list首元素前插入值为val的元素
pop_front删除list中第一个元素
push_back在list尾部插入值为val的元素
pop_back删除list中最后一个元素
insert在list position 位置中插入值为val的元素
erase 删除list position位置的元素
swap 交换两个list中的元素
clear 清空list中的有效元素
  1. // list插入和删除
  2. // push_back/pop_back/push_front/pop_front
  3. void TestList3()
  4. {
  5. int array[] = { 1, 2, 3 };
  6. list<int> L(array, array + sizeof(array) / sizeof(array[0]));
  7. // 在list的尾部插入4,头部插入0
  8. L.push_back(4);
  9. L.push_front(0);
  10. PrintList(L);
  11. // 删除list尾部节点和头部节点
  12. L.pop_back();
  13. L.pop_front();
  14. PrintList(L);
  15. }
  16. // insert /erase
  17. void TestList4()
  18. {
  19. int array1[] = { 1, 2, 3 };
  20. list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  21. // 获取链表中第二个节点
  22. auto pos = ++L.begin();
  23. cout << *pos << endl;
  24. // 在pos前插入值为4的元素
  25. L.insert(pos, 4);
  26. PrintList(L);
  27. // 在pos前插入5个值为5的元素
  28. L.insert(pos, 5, 5);
  29. PrintList(L);
  30. // 在pos前插入[v.begin(), v.end)区间中的元素
  31. vector<int> v{ 7, 8, 9 };
  32. L.insert(pos, v.begin(), v.end());
  33. PrintList(L);
  34. // 删除pos位置上的元素
  35. L.erase(pos);
  36. PrintList(L);
  37. // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
  38. L.erase(L.begin(), L.end());
  39. PrintList(L);
  40. }
  41. // resize/swap/clear
  42. void TestList5()
  43. {
  44. // 用数组来构造list
  45. int array1[] = { 1, 2, 3 };
  46. list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  47. PrintList(l1);
  48. // 交换l1和l2中的元素
  49. list<int> l2;
  50. l1.swap(l2);
  51. PrintList(l1);
  52. PrintList(l2);
  53. // 将l2中的元素清空
  54. l2.clear();
  55. cout << l2.size() << endl;
  56. }

1.2.6 list operations

函数声明 接口说明

splice

从一个列表转移元素到另一个列表

remove

移除具有特定值的元素

unique

移除重复的数值

merge

合并排序过的列表

sort

对容器中的元素进行排序

reverse

颠倒元素的顺序
  1. int main()
  2. {
  3. list<int> lt;
  4. lt.push_back(1);
  5. lt.push_back(2);
  6. lt.push_back(3);
  7. lt.push_back(4);
  8. lt.push_back(5);
  9. list<int>::iterator it = lt.begin();
  10. //while (it < lt.begin())
  11. //这里不能使用 < ,因为不能保证前一个节点比后一个大节点
  12. //链表间没有大小关系
  13. // < 只适用于底层物理空间连续:string,vector
  14. while (it != lt.end())
  15. {
  16. cout << *it << " ";
  17. ++it;
  18. }
  19. cout << endl;
  20. //逆置链表
  21. lt.reverse();
  22. for (auto e : lt)
  23. {
  24. cout << e << " ";
  25. }
  26. cout << endl;
  27. //归并链表
  28. //归并的前提:要求两个链表都有序
  29. list<int> lt1;
  30. lt1.push_back(1);
  31. lt1.push_back(2);
  32. lt1.push_front(1);
  33. // lt和lt1链表排序
  34. lt.sort();
  35. lt1.sort();
  36. lt.merge(lt1);
  37. for (auto e : lt)
  38. {
  39. cout << e << " ";
  40. }
  41. cout << endl;
  42. //链表去重 - 要求 - 链表有序/重复元素相邻
  43. lt.unique();
  44. for (auto e : lt)
  45. {
  46. cout << e << " ";
  47. }
  48. cout << endl;
  49. //链表排序
  50. lt.sort();
  51. for (auto e : lt)
  52. {
  53. cout << e << " ";
  54. }
  55. cout << endl;
  56. //这里提一个问题,能不能使用算法库的sort
  57. //这里为什么要单独提供一个链表的sort
  58. /*sort(lt.begin(), lt.end());
  59. for (auto e : lt)
  60. {
  61. cout << e << " ";
  62. }
  63. cout << endl;*/
  64. //error:_Sort_unchecked(_UFirst, _ULast, _ULast - _UFirst, _Pass_fn(_Pred));
  65. //算法库实现的sort是两个迭代器相减,list物理空间不连续,不支持相减
  66. //erase:删除pos位置的值
  67. //remove:根据值找pos位置+删除pos位置的值
  68. lt.remove(4);
  69. for (auto e : lt)
  70. {
  71. cout << e << " ";
  72. }
  73. cout << endl;
  74. //链表转移 - 节点转移
  75. list<int> mylist1, mylist2;
  76. list<int>::iterator it;
  77. // set some initial values:
  78. for (int i = 1; i <= 4; ++i)
  79. mylist1.push_back(i); // mylist1: 1 2 3 4
  80. for (int i = 1; i <= 3; ++i)
  81. mylist2.push_back(i * 10);// mylist2: 10 20 30
  82. it = mylist1.begin();
  83. ++it; // points to 2
  84. mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
  85. // mylist2 (empty)
  86. //LRU,2最近被使用到 - 最少使用次数
  87. lt.splice(lt.end(), lt, find(lt.begin(), lt.end(), 2));
  88. for (auto e : lt)
  89. {
  90. cout << e << " ";
  91. }
  92. cout << endl;
  93. return 0;
  94. }

2. list的模拟实现

2.1 特点再次介绍

  • 1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代
  • 2. list的底层是带头双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  • 3. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好
  • 4. 与其他序列式容器相比,list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】

推荐阅读
相关标签