• 【C++历练之路】list的重要接口||底层逻辑的三个封装以及模拟实现


    W...Y的主页 😊

    代码仓库分享💕 


    🍔前言:

    在C++的世界中,有一种数据结构,它不仅像一个神奇的瑰宝匣,还像一位能够在数据的海洋中航行的智慧舵手。这就是C++中的list,一个引人入胜的工具,它以一种优雅而强大的方式管理着数据的舞台。想象一下,你有一个能够轻松操纵、轻松操作的魔法列表,让你的编程之旅变得轻松而令人愉悦。让我们一同揭开list的神秘面纱,深入探索这个双向链表的奇妙世界。

    目录

    list的介绍及使用

    list的介绍

     list的使用

     list的构造

    list iterator的使用

    list capacity

    list element access

     list modifiers

    list的模拟实现

    模拟实现list的准备

    封装节点——第一个封装

    创建list类——第二个封装

    push_back函数模拟

    创建迭代器类——第三个封装 

     begin与end函数模拟

    insert函数模拟实现

    erase函数模拟实现

    clear函数以及析构函数的实现

    其余函数接口的实现 


    list的介绍及使用

    list的介绍

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

    list的文档介绍 icon-default.png?t=N7T8https://legacy.cplusplus.com/reference/list/list/

     list的使用

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

     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. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. #include
    5. #include
    6. // list的构造
    7. void TestList1()
    8. {
    9. list<int> l1; // 构造空的l1
    10. list<int> l2(4, 100); // l2中放4个值为100的元素
    11. list<int> l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
    12. list<int> l4(l3); // 用l3拷贝构造l4
    13. // 以数组为迭代器区间构造l5
    14. int array[] = { 16,2,77,29 };
    15. list<int> l5(array, array + sizeof(array) / sizeof(int));
    16. // 列表格式初始化C++11
    17. list<int> l6{ 1,2,3,4,5 };
    18. // 用迭代器方式打印l5中的元素
    19. list<int>::iterator it = l5.begin();
    20. while (it != l5.end())
    21. {
    22. cout << *it << " ";
    23. ++it;
    24. }
    25. cout << endl;
    26. // C++11范围for的方式遍历
    27. for (auto& e : l5)
    28. cout << e << " ";
    29. cout << endl;
    30. }

    list的构造与STL中vector、string构造大同小异,都是有构造空对象,构造的list中包含n个值为val的元素,拷贝构造以及迭代器构造。 

    list iterator的使用

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

    函数声明接口说明
    begin+end返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
    rbegin+rend返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即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::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::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. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
    2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

    3.迭代器都会提供两个版本,一个是无const修饰的,一个是有const修饰的

    list capacity

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

    1. // list::empty
    2. #include
    3. #include
    4. int main ()
    5. {
    6. std::list<int> mylist;
    7. int sum (0);
    8. for (int i=1;i<=10;++i) mylist.push_back(i);
    9. while (!mylist.empty())
    10. {
    11. sum += mylist.front();
    12. mylist.pop_front();
    13. }
    14. std::cout << "total: " << sum << '\n';
    15. return 0;
    16. }

    1. // list::size
    2. #include
    3. #include
    4. int main ()
    5. {
    6. std::list<int> myints;
    7. std::cout << "0. size: " << myints.size() << '\n';
    8. for (int i=0; i<10; i++) myints.push_back(i);
    9. std::cout << "1. size: " << myints.size() << '\n';
    10. myints.insert (myints.begin(),10,100);
    11. std::cout << "2. size: " << myints.size() << '\n';
    12. myints.pop_back();
    13. std::cout << "3. size: " << myints.size() << '\n';
    14. return 0;
    15. }

     这两个函数都是与list中成员有关的函数,我们学会后可以方便快速使用。

    list element access

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

    1. // list::front
    2. #include
    3. #include
    4. int main ()
    5. {
    6. std::list<int> mylist;
    7. mylist.push_back(77);
    8. mylist.push_back(22);
    9. // now front equals 77, and back 22
    10. mylist.front() -= mylist.back();
    11. std::cout << "mylist.front() is now " << mylist.front() << '\n';
    12. return 0;
    13. }

    1. // list::back
    2. #include
    3. #include
    4. int main ()
    5. {
    6. std::list<int> mylist;
    7. mylist.push_back(10);
    8. while (mylist.back() != 0)
    9. {
    10. mylist.push_back ( mylist.back() -1 );
    11. }
    12. std::cout << "mylist contains:";
    13. for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end() ; ++it)
    14. std::cout << ' ' << *it;
    15. std::cout << '\n';
    16. return 0;
    17. }

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

    这些都是list中一些重要接口,我们一定要牢记。list中还有一些操作,需要用到时大家可参阅list的文档说明。

    list的模拟实现

    模拟实现list的准备

    要模拟实现list,必须要熟悉list的底层结构以及其接口的含义,所以我们先从STL源码(SGI版本)开始学习。我们要进行模拟,首先得知道底层的数据类型都有什么。

    首先我们知道list是带头双向链表,所以每一处都有一个节点,所以C++肯定会对节点进行封装。

    源码中创建了节点的模板,使用struct对节点进行封装处理。因为我们要访问节点,所以使用struct进行类定义而不是class,class默认类部成员都是私有,struct默认类部成员都是公有。

    接下来应该看list的结构,看list中的成员变量有什么?

      list类中只有一个成员,并且这个成员是节点的指针。

    我们已经大致了解了list的类型,接下来我们开始模拟实现list。

    封装节点——第一个封装

    1. #include
    2. #include
    3. using namespace std;
    4. namespace why
    5. {
    6. template<class T>
    7. struct list_node
    8. {
    9. list_node* _next;
    10. list_node* _prev;
    11. T _data;
    12. list_node(const T& x = T())
    13. :_next(nullptr)
    14. , _prev(nullptr)
    15. , _data(x)
    16. {}
    17. };

    创建一个节点类进行封装, 我们在这里没有源码中那么繁琐,不需要定义空指针进行强制类型转换,而是直接使用list_node*进行指针声明。在这里我们也需要构造函数,默认构造函数对指针不能很好的初始化。

    创建list类——第二个封装

    1. template<class T>
    2. class list
    3. {
    4. typedef list_node node;
    5. public:
    6. typedef __list_iterator iterator;
    7. typedef __list_iteratorconst T&,const T*> const_iterator;
    8. list()
    9. {
    10. _head = new node;
    11. _head->_next = _head;
    12. _head->_prev = _head;
    13. }
    14. private
    15. node* _head;
    16. };

    list是一个双向循环链表,所以它只需要一个指针,便可以遍历整个链表并且回到原来的位置。为此我们可以设计一个头节点为list的起始节点,这个头节点不含任何数据,它只是作为一个空的节点而已,所以我们创建一个_head指针作为头节点。

    push_back函数模拟

    push_back函数是在list的末尾进行插入数据,就与C语言中的数据结构一样进行插入即可。

    1. void push_back(const T& x)
    2. {
    3. node* tail = _head->_prev;
    4. //创建新节点
    5. node* newnode = new node(x);
    6. tail->_next = newnode;
    7. newnode->_prev = tail;
    8. newnode->_next = _head;
    9. _head->_prev = newnode;
    10. }

    创建一个新节点,让新节点的_prev指向尾部,_next指向头节点。让头节点的_prev指向新节点,尾部节点的_next指向新节点即可。

    创建迭代器类——第三个封装 

    我们现在已经可以将数据尾插到list中去了,但是如何进行遍历打印呢?在list中因为每一个节点的空间是不连续的,所以不能重载[]进行下标访问。而且在string与vector中都使用的是原生指针,所以可以进行++,!=,*()操作,但是在list中却不能使用。因此list的迭代器应该是自定义类型对原生指针的封装,模拟指针的行为,才能有正确的递增,递减,取值,成员取用的行为。

    我们需要通过源码进行分析,然后创建一个迭代器的类自己进行重载正确使用。这里推荐大家去看一下源码。

    SGI版本list源码icon-default.png?t=N7T8https://github.com/karottc/sgi-stl

     总结如下:

    递增:正确的找到其next的地址

    递减:正确的找到其prev的地址

    取值:当前节点的取值

    成员取用:当前节点的成员

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

     类模板中为什么有三个参数呢?因为*()与->进行重载时会面临两种情况,有无const修饰,所以我们可以通过模板来传递此重载是否有无const。如果不理解这种情况,我们可以分开写有无const的情况,但是这样需要写4种情况,造成代码的冗余。

    这时我们就可以使用迭代器对list进行遍历打印操作了,可以使用范围for。

    1. void test1()
    2. {
    3. list<int> ll;
    4. ll.push_back(1);
    5. ll.push_back(1);
    6. ll.push_back(1);
    7. ll.push_back(1);
    8. list<int>::iterator it = ll.begin();
    9. while (it != ll.end())
    10. {
    11. cout << *it << ' ';
    12. ++it;
    13. }
    14. cout << endl;
    15. }

     注意:这里我们在给迭代器it赋值时调用了默认拷贝构造函数,因为这里不需要深拷贝。但是在vector,string的情况来说浅拷贝会报错,但是这里为什么没有报错呢?因为在迭代器类中我们并没有写析构函数,所以不会进行多次重复释放空间。

    我们这里是不需要写析构函数的,因为迭代器创建的类只是为了更好的适应迭代器的操作,因为list是不连续的空间,我们迭代器指向的空间全部都是节点的,我们只是使用一下而已不需要进行释放操作,释放是list的事情。

     begin与end函数模拟

    1. iterator begin()
    2. {
    3. //iterator it(_head->_next);
    4. //return it;
    5. return iterator(_head->_next);
    6. }
    7. const_iterator begin() const
    8. {
    9. return const_iterator(_head->_next);
    10. }
    11. iterator end()
    12. {
    13. return iterator(_head);
    14. }
    15. const_iterator end() const
    16. {
    17. //iterator it(_head->_next);
    18. //return it;
    19. return const_iterator(_head);
    20. }

    begin与end都有两个版本,const与非const。

    insert函数模拟实现

    插入函数非常简单,在迭代器pos位置进行插入即可。

    1. void insert(iterator pos, const T& x)
    2. {
    3. node* cur = pos._node;
    4. node* prev = cur->_prev;
    5. node* newnode = new node(x);
    6. prev->_next = newnode;
    7. newnode->_prev = prev;
    8. cur->_prev = newnode;
    9. newnode->_next = cur;
    10. }

    将插入的数进行前端后端相连即可。

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

    erase函数模拟实现

     我们可以看出erase是有返回值的,就是避免迭代器失效的原因,而且绝对不能删除哨兵位节点,所以得使用断言。

    1. iterator erase(iterator pos)
    2. {
    3. assert(pos != end());
    4. node* prev = pos._node->_prev;
    5. node* cur = pos._node->_next;
    6. prev->_next = cur;
    7. cur->_prev = prev;
    8. delete pos._node;
    9. return iterator(cur);
    10. }

     写完insert与erase我们就可以对其进行复用,pop_back、pop_front、push_back、push_front就是进行了首插、首删、尾插、尾删。

    1. void pop_back()
    2. {
    3. erase(--end());
    4. }
    5. void pop_front()
    6. {
    7. erase(begin());
    8. }
    9. void push_back(const T& x)
    10. {
    11. insert(end(), x);
    12. }
    13. void push_front(const T& x)
    14. {
    15. insert(begin(), x);
    16. }

    clear函数以及析构函数的实现

    clear函数就是将list置空,所以我们可以复用erase进行逐一删除即可。

    1. void clear()
    2. {
    3. iterator it = begin();
    4. while (it != end())
    5. {
    6. erase(it++);
    7. //it = erase(it);
    8. }
    9. }

    不能使用erase(it),会导致迭代器失效 

    析构函数将空间全部释放置空:

    1. ~list()
    2. {
    3. clear();
    4. delete _head;
    5. _head = nullptr;
    6. }

    其余函数接口的实现 

    迭代器初始化:

    1. template <class Iterator>
    2. list(Iterator first, Iterator last)
    3. {
    4. empty_init();
    5. while (first != last)
    6. {
    7. push_back(*first);
    8. ++first;
    9. }
    10. }

    拷贝构造函数:

    1. void swap(list& tmp)
    2. {
    3. std::swap(_head, tmp._head);
    4. }
    5. list(const list& lt)
    6. {
    7. _head = new node;
    8. _head->_next = _head;
    9. _head->_prev = _head;
    10. list tmp(lt.begin(), lt.end());
    11. swap(tmp);
    12. }

     我们使用现代写法进行偷懒,我们使用迭代器初始化一个临时对象tmp,将tmp与目标进行交换即可。

    赋值重载构造:

    1. list<int>& operator=(list lt)
    2. {
    3. swap(lt);
    4. return *this;
    5. }

    我们使用传值时会进行拷贝构造临时对象lt,将lt与目标进行交换即可,属于窃取劳动成果! 


    在我们的"C++ List探秘之旅"中,我们像是一群探险者,勇敢地穿越了C++编程的密林,发现了list这个神奇的宝藏。现在,当我们回望这段旅程时,或许你已经领略到了在数据操控的掌声中,list是如何成为代码交响乐团的一部分。这并不是终点,而是一个新的起点。在C++的舞台上,list为你打开了通往更高层次编程乐趣的大门,希望大家可以通过本文走的更高。感谢观看!!!

  • 相关阅读:
    Nexus 私服上传 jar 包 Connection rest
    n皇后实现总结
    C/C++数据结构——[NOIP2004]FBI树(二叉树)
    初级程序员如何进阶
    Neo4j图数据库_web页面关闭登录实现免登陆访问_常用的cypher语句_删除_查询_创建关系图谱---Neo4j图数据库工作笔记0013
    C++继承和派生的基本概念
    MQTT X v1.8.3 正式发布
    java计算机毕业设计汉语言类网上考试系统MyBatis+系统+LW文档+源码+调试部署
    Polygon zkEVM的Dragon Fruit和Inca Berry升级
    【Linux】Ubuntu存储分析
  • 原文地址:https://blog.csdn.net/m0_74755811/article/details/134486911