当前位置:   article > 正文

【C++】STL之list类源码剖析_stl list push_back源码

stl list push_back源码

目录

概述

源码

iterator.h

MyList.h

test.cpp

测试结果


概述

STL标准库中的list类的本质是一个带头双向链表,支持头部增删和尾部增删,以及随机增删,但是不支持随机访问。

list的遍历是通过迭代器进行遍历,迭代器分为正向和反向迭代器,也有const迭代器,反向迭代器是通过正向迭代器实现的。

list的构造能通过迭代器进行构造,也是通过自定义swap函数利用临时变量简化代码完成构造。

源码

iterator.h

  1. #pragma once
  2. template<class T>
  3. struct Node
  4. {
  5. T _data;
  6. Node<T>* _next;
  7. Node<T>* _prev;
  8. Node(const T& x)
  9. : _data(x), _next(nullptr), _prev(nullptr)
  10. {}
  11. };
  12. template<class T, class Ref, class Ptr>
  13. class ListIterator
  14. {
  15. public:
  16. typedef Node<T> node;
  17. typedef ListIterator<T, Ref, Ptr> Self;
  18. node* _pnode;
  19. ListIterator(node* p)
  20. : _pnode(p)
  21. {}
  22. Ref operator*()
  23. {
  24. return _pnode->_data;
  25. }
  26. Ptr operator->()
  27. {
  28. return &(_pnode->_data);
  29. }
  30. Self& operator++()
  31. {
  32. _pnode = _pnode->_next;
  33. return *this;
  34. }
  35. Self operator++(int)
  36. {
  37. Self tmp(*this);
  38. _pnode = _pnode->_next;
  39. return tmp;
  40. }
  41. Self& operator--()
  42. {
  43. _pnode = _pnode->_prev;
  44. return *this;
  45. }
  46. Self operator--(int)
  47. {
  48. Self tmp(*this);
  49. _pnode = _pnode->_prev;
  50. return tmp;
  51. }
  52. bool operator==(const Self& it)const
  53. {
  54. return _pnode == it._pnode;
  55. }
  56. bool operator!=(const Self& it)const
  57. {
  58. return _pnode != it._pnode;
  59. }
  60. };
  61. // 通过正向迭代器,构造出反向迭代器
  62. template<class Iterator, class Ref, class Ptr>
  63. class ReverseIterator
  64. {
  65. public:
  66. Iterator _it;
  67. typedef ReverseIterator<Iterator, Ref, Ptr> Self;
  68. ReverseIterator(Iterator it)
  69. : _it(it)
  70. {}
  71. Ref operator*()
  72. {
  73. Iterator tmp = _it;
  74. return *(--tmp);
  75. }
  76. Ptr operator->()
  77. {
  78. return &(operator*());
  79. }
  80. Self& operator++()
  81. {
  82. --_it;
  83. return *this;
  84. }
  85. Self operator++(int)
  86. {
  87. Self tmp(*this);
  88. --_it;
  89. return tmp;
  90. }
  91. Self& operator--()
  92. {
  93. ++_it;
  94. return *this;
  95. }
  96. Self operator--(int)
  97. {
  98. Self tmp(*this);
  99. ++_it;
  100. return tmp;
  101. }
  102. bool operator==(const Self& s)const
  103. {
  104. return _it == s._it;
  105. }
  106. bool operator!=(const Self& s)const
  107. {
  108. return _it != s._it;
  109. }
  110. };

MyList.h

  1. #pragma once
  2. #include <iostream>
  3. #include <cassert>
  4. #include "iterator.h"
  5. template<class T>
  6. class List
  7. {
  8. typedef Node<T> node;
  9. public:
  10. typedef ListIterator<T, T&, T*> iterator;
  11. typedef ListIterator<T, const T&, const T*> const_iterator;
  12. typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
  13. typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;
  14. iterator begin()
  15. {
  16. return iterator(_head->_next);
  17. }
  18. iterator end()
  19. {
  20. return iterator(_head);
  21. }
  22. const_iterator begin()const
  23. {
  24. return const_iterator(_head->_next);
  25. }
  26. const_iterator end()const
  27. {
  28. return const_iterator(_head);
  29. }
  30. reverse_iterator rbegin()
  31. {
  32. return reverse_iterator(end());
  33. }
  34. reverse_iterator rend()
  35. {
  36. return reverse_iterator(begin());
  37. }
  38. const_reverse_iterator rbegin()const
  39. {
  40. return const_reverse_iterator(end());
  41. }
  42. const_reverse_iterator rend()const
  43. {
  44. return const_reverse_iterator(begin());
  45. }
  46. // 迭代器构造
  47. template<class InputIterator>
  48. List(InputIterator first, InputIterator last)
  49. {
  50. empty_initial();
  51. while (first != last)
  52. {
  53. push_back(*first);
  54. ++first;
  55. }
  56. }
  57. void swap(List<T>& lt)
  58. {
  59. std::swap(_head, lt._head);
  60. std::swap(_size, lt._size);
  61. }
  62. void empty_initial()
  63. {
  64. _head = new node(T());
  65. _head->_next = _head;
  66. _head->_prev = _head;
  67. _size = 0;
  68. }
  69. List()
  70. {
  71. empty_initial();
  72. }
  73. List(const List<T>& lt)
  74. {
  75. empty_initial();
  76. List<T> tmp(lt.begin(), lt.end());
  77. swap(tmp);
  78. }
  79. List<T>& operator=(List<T> lt)
  80. {
  81. swap(lt);
  82. return *this;
  83. }
  84. ~List()
  85. {
  86. clear();
  87. delete _head;
  88. _head = nullptr;
  89. }
  90. void clear()
  91. {
  92. iterator it = begin();
  93. while (it != end())
  94. {
  95. it = erase(it);
  96. }
  97. }
  98. size_t size()const
  99. {
  100. return _size;
  101. }
  102. bool empty()const
  103. {
  104. return _size == 0;
  105. }
  106. void push_back(const T& x)
  107. {
  108. insert(end(), x);
  109. }
  110. void push_front(const T& x)
  111. {
  112. insert(begin(), x);
  113. }
  114. void pop_back()
  115. {
  116. erase(--end());
  117. }
  118. void pop_front()
  119. {
  120. erase(begin());
  121. }
  122. // 在pos处前插
  123. iterator insert(iterator pos, const T& x)
  124. {
  125. node* newNode = new node(x);
  126. node* cur = pos._pnode;
  127. node* prev = cur->_prev;
  128. prev->_next = newNode;
  129. newNode->_prev = prev;
  130. newNode->_next = cur;
  131. cur->_prev = newNode;
  132. ++_size;
  133. return iterator(newNode);
  134. }
  135. iterator erase(iterator pos)
  136. {
  137. assert(pos != end());
  138. node* prev = pos._pnode->_prev;
  139. node* next = pos._pnode->_next;
  140. prev->_next = next;
  141. next->_prev = prev;
  142. delete pos._pnode;
  143. pos._pnode = nullptr;
  144. --_size;
  145. return iterator(next);
  146. }
  147. private:
  148. node* _head;
  149. size_t _size;
  150. };

test.cpp

  1. #include "MyList.h"
  2. template<class T>
  3. void print_list(const List<T>& lt)
  4. {
  5. std::cout << "size=" << lt.size() << ": ";
  6. for (auto& e : lt)
  7. {
  8. std::cout << e << ' ';
  9. }
  10. std::cout << std::endl;
  11. }
  12. void test()
  13. {
  14. std::cout << "构建lt1 ";
  15. List<int> lt1;
  16. lt1.push_back(2);
  17. lt1.push_back(4);
  18. lt1.push_back(6);
  19. lt1.push_back(8);
  20. lt1.push_front(1);
  21. lt1.push_front(3);
  22. lt1.push_front(5);
  23. lt1.push_front(7);
  24. print_list(lt1);
  25. std::cout << "lt1前删/后删 ";
  26. lt1.pop_front();
  27. lt1.pop_back();
  28. print_list(lt1);
  29. std::cout << "拷贝构造lt2/迭代器 ";
  30. List<int> lt2(lt1);
  31. List<int>::iterator it2 = lt2.begin();
  32. while (it2 != lt2.end())
  33. {
  34. std::cout << *it2 << ' ';
  35. ++it2;
  36. }
  37. std::cout << std::endl;
  38. std::cout << "赋值构造lt3/反向迭代器 ";
  39. List<int> lt3 = lt1;
  40. List<int>::reverse_iterator rit3 = lt3.rbegin();
  41. while (rit3 != lt3.rend())
  42. {
  43. std::cout << *rit3 << ' ';
  44. ++rit3;
  45. }
  46. std::cout << std::endl;
  47. std::cout << "清空lt3 ";
  48. lt3.clear();
  49. print_list(lt3);
  50. std::cout << "自定义结构体链表 ";
  51. struct Pos
  52. {
  53. int _row, _col;
  54. Pos(int row = 0, int col = 0)
  55. : _row(row), _col(col)
  56. {}
  57. };
  58. List<Pos> lt4;
  59. lt4.push_back(Pos(1, 1));
  60. lt4.push_back(Pos(2, 2));
  61. lt4.push_back(Pos(3, 3));
  62. lt4.push_front(Pos(0, 0));
  63. std::cout << "size=" << lt4.size() << ": ";
  64. List<Pos>::iterator it4 = lt4.begin();
  65. while (it4 != lt4.end())
  66. {
  67. ++(it4->_row);
  68. std::cout << "(" << it4->_row << "," << it4->_col << ")" << " ";
  69. ++it4;
  70. }
  71. std::cout << std::endl;
  72. std::cout << "拷贝自定义结构体链表 ";
  73. const List<Pos> lt5(lt4);
  74. std::cout << "size=" << lt5.size() << ": ";
  75. List<Pos>::const_iterator cit5 = lt5.begin();
  76. while (cit5 != lt5.end())
  77. {
  78. std::cout << "(" << cit5->_row << "," << cit5->_col << ")" << " ";
  79. ++cit5;
  80. }
  81. std::cout << std::endl;
  82. std::cout << "反向迭代器 ";
  83. List<Pos>::const_reverse_iterator rcit5 = lt5.rend();
  84. while (rcit5 != lt5.rbegin())
  85. {
  86. --rcit5;
  87. std::cout << "(" << rcit5->_row << "," << rcit5->_col << ")" << " ";
  88. }
  89. std::cout << std::endl;
  90. }
  91. int main()
  92. {
  93. test();
  94. return 0;
  95. }

测试结果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/128137
推荐阅读
相关标签
  

闽ICP备14008679号