0


STL——list

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来说这可能是一个重要的因素)

2、list用法

list的接口有很多,具体如下

具体用法可以查看 https://legacy.cplusplus.com/reference/list/list/?kw=list

下面我介绍一些常用的接口的用法

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. }

2.2list iterator****的使用

迭代器底层是使用指针实现的,所以,我们可以把迭代器当成一个指针,指向list的某个结点。所有的容器的迭代器都被重命名为iterator

函数声明

**接口说明 **

begin +**end **

返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器

rbegin +

**rend **

*返回第一个元素的*reverse_iterator,end**位置返回最后一个元素下一个位置的 **

*reverse_iterator,begin***位置 **

**【注意】 **

  1. beginend**为正向迭代器,对迭代器执行++****操作,迭代器向后移动 **

  2. **rbegin(end)rend(begin)为反向迭代器,对迭代器执行++**操作,迭代器向前移动

  1. //iterator
  2. void PrintList(const list<int>& l)
  3. {
  4. for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
  5. {
  6. cout << *it << " ";
  7. // *it = 10; 这里是const_iterator ,it指向的内容不能被修改,所以编译不通过
  8. }
  9. cout << endl;
  10. }
  11. void TestList2()
  12. {
  13. int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  14. list<int> l(array, array + sizeof(array) / sizeof(array[0]));
  15. // 使用正向迭代器正向list中的元素
  16. // list<int>::iterator it = l.begin();
  17. auto it = l.begin();
  18. while (it != l.end())
  19. {
  20. cout << *it << " ";
  21. ++it;
  22. }
  23. cout << endl;
  24. // 使用反向迭代器逆向打印list中的元素
  25. // list<int>::reverse_iterator rit = l.rbegin();
  26. auto rit = l.rbegin();
  27. while (rit != l.rend())
  28. {
  29. cout << *rit << " ";
  30. ++rit;
  31. }
  32. cout << endl;
  33. }

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

2.4list****的迭代器失效

前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

  1. //这是一个模拟List的clear的实现
  2. //这是错误写法
  3. void clear()
  4. {
  5. iterator it = begin();
  6. while (it != end())
  7. {
  8. erase(it);
  9. it++;
  10. }
  11. }
  12. //这段代码之所以错误,主要是因为,it指向的那个结点已经被删除了,所以it再++是找不到后面节点的位置的。
  13. ///
  14. //正确写法
  15. void clear()
  16. {
  17. iterator it = begin();
  18. while (it != end())
  19. {
  20. it=erase(it);//删除这个结点,返回这个结点的下一个位置给it。
  21. }
  22. }

3、list****的模拟实现

3、1node类模板

  1. #include<iostream>
  2. #include<assert.h>
  3. using namespace std;
  4. namespace A
  5. {
  6. //用命名区间A把自己实现的list圈起来,防止与库里面的混淆
  7. ........
  8. }

首先先构造出结点的类模板list_node<T>,因为struct默认所有成员都是public,我们需要在类外面使用list_node,所以这里使用struct。定义三个成员变量 _next下一个节点,_pre上一个结点和数据data。

  1. template <class T>
  2. struct list_node
  3. {
  4. typedef list_node<T> Node;
  5. Node* _next;
  6. Node* _pre;
  7. T _data;
  8. list_node(const T& val=T())
  9. :_next(nullptr)
  10. ,_pre(nullptr)
  11. ,_data(val)
  12. {
  13. }
  14. };

3、2迭代器类模板

l链表的物理结构并不是连续的,它不像string、vector的结构,对list进行++时找不到它的下一个结点的。所以我们必须自己模拟出它的迭代器。

定义一个迭代器类模板,在里面服用list_node类,定义出迭代器的成员变量_node。在里面实现出我们需要用的运算符

  1. template<class T,class Ref,class Ptr>
  2. struct __list_iterator
  3. {
  4. typedef list_node<T> Node;
  5. typedef __list_iterator<T,Ref,Ptr> self;
  6. __list_iterator(Node* node)
  7. :_node(node)
  8. {
  9. }
  10. Node* _node;
  11. self& operator++()
  12. {
  13. _node = _node->_next;
  14. return *this;
  15. }
  16. self operator++(int)
  17. {
  18. self tmp(*this);
  19. _node = _node->_next;
  20. return tmp;
  21. }
  22. self& operator--()
  23. {
  24. _node = _node->_pre;
  25. return *this;
  26. }
  27. self operator--(int)
  28. {
  29. self tmp(*this);
  30. _node = _node->pre;
  31. return tmp;
  32. }
  33. Ref operator*()
  34. {
  35. return _node->_data;
  36. }
  37. Ptr operator->()
  38. {
  39. return &_node->_data;
  40. }
  41. bool operator != (const self& s)
  42. {
  43. return _node != s._node;
  44. }
  45. };

3、3list类模板

实现出list的迭代器,我们就可以正式来模拟list接口了。

首先定义一个哨兵位结点,这也是list类模板唯一的成员变量。在此我们复用list_node类模板和迭代器类模板,然后我们实现list的各个接口

  1. template<class T>
  2. class list
  3. {
  4. typedef list_node<T> Node;
  5. public:
  6. typedef __list_iterator<T,T&,T*> iterator;
  7. typedef __list_iterator<T,const T&,const T*> const_iterator;
  8. void init()
  9. {
  10. _head = new Node;
  11. _head->_next = _head;
  12. _head->_pre = _head;
  13. }
  14. list()
  15. {
  16. init();
  17. }
  18. void clear()
  19. {
  20. iterator it = begin();
  21. while (it != end())
  22. {
  23. it=erase(it);
  24. }
  25. }
  26. ~list()
  27. {
  28. clear();
  29. delete _head;
  30. _head = nullptr;
  31. }
  32. void swap(list<T>& lt)
  33. {
  34. std::swap(_head, lt._head);
  35. }
  36. list(const list<T>& lt)//构造
  37. {
  38. _head = new Node;
  39. _head->_next = _head;
  40. _head->_pre = _head;
  41. for (auto e : lt)
  42. {
  43. push_back(e);
  44. }
  45. }
  46. list<T>& operator=(list<T> lt)
  47. {
  48. swap(lt);
  49. return *this;
  50. }
  51. void push_back(const T& x)
  52. {
  53. Node* tail = _head->_pre;
  54. Node* newnode = new Node(x);
  55. tail->_next = newnode;
  56. newnode->_pre = tail;
  57. newnode->_next = _head;
  58. _head->_pre = newnode;
  59. }
  60. void push_front(const T& x)
  61. {
  62. insert(begin(), x);
  63. }
  64. iterator insert(iterator pos,const T& x)
  65. {
  66. Node* newnode = new Node(x);
  67. Node* cur = pos._node;
  68. Node* pre = cur->_pre;
  69. pre->_next = newnode;
  70. newnode->_pre = pre;
  71. newnode->_next = cur;
  72. cur->_pre = newnode;
  73. return newnode;
  74. }
  75. iterator erase(iterator pos)
  76. {
  77. assert(pos != end());
  78. Node* cur = pos._node;
  79. Node* pre = cur->_pre;
  80. Node* next = cur->_next;
  81. pre->_next=next;
  82. next->_pre = pre;
  83. delete cur;
  84. return next;
  85. }
  86. void pop_back()
  87. {
  88. erase(--end());
  89. }
  90. void pop_front()
  91. {
  92. erase(begin());
  93. }
  94. iterator begin()
  95. {
  96. return _head->_next;
  97. }
  98. iterator end()
  99. {
  100. return _head;
  101. }
  102. const_iterator begin() const
  103. {
  104. return _head->_next;
  105. }
  106. const_iterator end() const
  107. {
  108. return _head;
  109. }
  110. private:
  111. Node* _head;
  112. };

上面就是list的常见接口的模拟实现,有些并不常见的我没有在此写出原来,如果以后见到的时候大家查一下List的文档就可以了,使用方法都很简单。

4、list的优缺点

带头结点的双向循环链表,list这个容器常用于适合大量插入删除数据的场景,由于它是一个个结点链接,所以它移动节点会很方便,并不需要挪动数据,头插头删,或者任意位置插入删除都很高效。但是它的缺点也很明显:不支持随机访问,访问某个元素效率O(N),底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低。大家使用的时候注意能否适合使用List。

标签: c++ STL list

本文转载自: https://blog.csdn.net/weixin_67131528/article/details/135720579
版权归原作者 七街酒465 所有, 如有侵权,请联系我们删除。

“STL——list”的评论:

还没有评论