C++标准模板库--list

打印 上一主题 下一主题

主题 1855|帖子 1855|积分 5575

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
list

介绍

        list 是序列容器,允许在序列内的任何位置进行常量时间的插入和删除操作,以及两个方向的迭代。列表容器被实现为双向带头链表;双链表可以将它们包含的每个元素存储在差别且不相关的存储位置。排序是通过与前面元素的链接和反面元素的链接的每个元素的关联在内部保持的。它们与 forward_list非常相似:主要区别在于forward_list对象是单链表,因此它们只能向前迭代,以调换更小和更高效。与其他根本标准序列容器(array、vector和deque)相比,list 在容器内的任何位置插入、提取和移动元素(迭代器已经得到)方面通常体现更好,因此在大量使用列表的算法(如排序算法)中也体现更好。与其他序列容器相比,list 和 forward_list的主要缺点是它们无法通过位置直接访问元素;比方,要访问列表中的第六个元素,必须从已知位置(如开始或结束)迭代到该位置,这必要在两者之间的间隔上花费线性时间。它们还斲丧一些额外的内存来生存与每个元素相关联的链接信息(对于包含小元素的大型列表来说,这可能是一个告急因素)。
        根据官方对 list 的介绍,我们可以相识到 list 中使用的是双向带头链表,其优点有高效的元素插入和删除、双向遍历、迭代器失效题目较小、顺应性强等。 

使用

         list的构造

函数名称接口说明list (size_type n, const value_type& val = value_type())构造的list中包含n个值为val的元素list()构造空的listlist (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. }
复制代码
        list迭代器的使用

函数名称接口说明begin+end返回第一个元素的迭代器+返回末了一个元素的下一个位置的迭代器rbegin+rend返回末了一个元素的迭代器,即end-1,返回末了一个元素下一个位置的迭代器即begin-1位置 注意:
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. }
复制代码
        list的容量与首尾

函数名称接口说明empty检测 list 是否为空,是返回 true,否则返回 false。size返回list中的有效节点的个数。front返回list第一个节点中的值的引用back返回list末了一恶搞节点中的值的引用         list的增删查改

函数名称接口说明push_front在list首元素前插入值val的元素pop_front删除list中第一个元素push_back在list尾部插入值为val的元素pop_back删除list中末了一个元素insert在list中pos的位置中插入置为val的元素erase删除list中在pos位置的元素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的迭代器失效的,只有在删除时才会失效。而且失效的只是只想被删除节点的迭代器,其他迭代器不会受到影响。
代码演示:
  1. void TestListIterator1()
  2. {
  3.     int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  4.     list<int> l(array, array + sizeof(array) / sizeof(array[0]));
  5.     auto it = l.begin();
  6.     while (it != l.end())
  7.     {
  8.         // erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
  9.         l.erase(it);
  10.         ++it;
  11.     }
  12. }
  13. // 改正
  14. void TestListIterator()
  15. {
  16.     int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  17.     list<int> l(array, array + sizeof(array) / sizeof(array[0]));
  18.     auto it = l.begin();
  19.     while (it != l.end())
  20.     {
  21.         l.erase(it++); // it = l.erase(it);
  22.     }
  23. }
复制代码

list底层的模仿实现

框架:

  1.         template<class T>
  2.         struct list_node
  3.         {
  4.                 T _data;
  5.                 list_node<T>* _next;
  6.                 list_node<T>* _prev;
  7.         };
  8.         template<class T, class Ref, class Ptr>
  9.         struct list_iterator
  10.         {
  11.                 typedef list_node<T> Node;
  12.                 typedef list_iterator<T, Ref, Ptr> Self;
  13.                 Node* _node;
  14.         };
  15.         template<class T>
  16.         class list
  17.         {
  18.         //typedef后的Node受访问限定符的影响。
  19.                 typedef list_node<T> Node;
  20.         public:
  21.                 //迭代器
  22.                 typedef list_iterator<T, T&, T*> iterator;
  23.                 typedef list_iterator<T, const T&, const T*> const_iterator;
  24.         private:
  25.                 Node* _head;
  26.                 size_t _size;
  27.         };
复制代码
解析:

  • list_node 类:是一个模板结构,存储恣意类型 T 的数据。此中_data是存储节点的数据;_next是指向下一个节点的指针;_prev是指向前一个节点的指针。
  • list_iterator 类:是一个模板结构,使用三个类型参数 T、Ref(引用类型)、Ptr(指针类型)。此中Node:为 list_node<T>的重定名,方便在迭代器中使用;Self:为list_iterator<T, Ref, Ptr>的重定名,使得在迭代器中可以使用自身的类型;_node是指向当前迭代器所指向的节点。
  • list 类:是一个模板类,存储类型 T 的元素。此中Node:为 list_node<T>的重定名,便于使用;iterator:定义为 list_iterator<T, T&, T*>,表现指向 T 类型元素的可修改迭代器;const_iterator:定义为 list_iterator<T, const T&, const T*>,表现指向 T 类型元素的只读迭代器。_head:是指向链表头部的节点指针;_size:记录链表中节点的数目。
        本质上是以 list 类为主体,先封装了一个 list_node 类用于记录链表节点的值、前驱指针和后继指针;后封装了一个 list_iterator 迭代器,迭代器以三参数模板可以根据必要灵活调整类型,这里分别构造了平凡迭代器和 const 迭代器,满足了差别的访问需求;而且只需定义一个模板类 list_iterator,就能创建差别类型的迭代器,减少重复代码。
默认成员函数

  1.         template<class T>
  2.         struct list_node
  3.         {
  4.                 T _data;
  5.                 list_node<T>* _next;
  6.                 list_node<T>* _prev;
  7.                 //默认构造
  8.                 list_node(const T& x = T())
  9.                         :_data(x)
  10.                         ,_next(nullptr)
  11.                         ,_prev(nullptr)
  12.                 {}
  13.         };
  14.         template<class T, class Ref, class Ptr>
  15.         struct list_iterator
  16.         {
  17.                 typedef list_node<T> Node;
  18.                 typedef list_iterator<T, Ref, Ptr> Self;
  19.                 Node* _node;
  20.                 //带参构造
  21.                 list_iterator(Node* node)
  22.                         :_node(node)
  23.                 {}
  24.         };
  25.         template<class T>
  26.         class list
  27.         {
  28.                 typedef list_node<T> Node;
  29.         public:
  30.                 //初始化空链表
  31.                 void empty_init()
  32.                 {
  33.                         _head = new Node();
  34.                         _head->_next = _head;
  35.                         _head->_prev = _head;
  36.                         _size = 0;
  37.                 }
  38.                 //默认构造
  39.                 list()
  40.                 {
  41.                         empty_init();
  42.                 }
  43.                 //initializer_list
  44.                 list(initializer_list<T>& li)
  45.                 {
  46.                         empty_init();
  47.                         for (auto& e : li)
  48.                         {
  49.                                 push_back(e);
  50.                         }
  51.                 }
  52.                 //拷贝构造
  53.                 list(const list<T>& lt)
  54.                 {
  55.                         empty_init();
  56.                         for (auto& e : lt)
  57.                         {
  58.                                 push_back(e);
  59.                         }
  60.                 }
  61.                 //swap
  62.                 void swap(list<T>& lt)
  63.                 {
  64.                         std::swap(_head, lt._head);
  65.                         std::swap(_size, lt._size);
  66.                 }
  67.                 //赋值重载
  68.                 list<T>& operator=(list<T> lt)
  69.                 {
  70.                         swap(lt);
  71.                         return *this;
  72.                 }
  73.                 //clear
  74.                 void clear()
  75.                 {
  76.                         auto it = begin();
  77.                         while (it != end())
  78.                         {
  79.                                 it = erase(it);
  80.                         }
  81.                 }
  82.                 //析构函数
  83.                 ~list()
  84.                 {
  85.                         clear();
  86.                         delete _head;
  87.                         _head = nullptr;
  88.                 }
  89.         private:
  90.                 Node* _head;
  91.                 size_t _size;
  92.         };
复制代码
list_node 中的默认构造函数:


  • 默认参数:构造函数的参数 x 有一个默认值 T(),意味着如果没有提供值,节点的数据将被初始化为类型 T 的默认构造值(比方,对于内置类型,通常是 0)。
  • 成员初始化

    • _data 初始化为 x,允许用户在创建节点时传入特定值。
    • _next 和 _prev 初始化为 nullptr,表现新节点在链表中尚未毗连到其他节点。

list_iterator 中的构造函数:


  • 带参构造:该构造函数接受一个指向 list_node 的指针,并将其赋值给成员变量 _node。这允许用户创建一个指向特定节点的迭代器,便于在链表中遍历。
 list 中的默认成员函数:


  • 默认构造函数空链表初始化,调用 empty_init() 方法,创建一个空的链表头节点,并将头节点的 _next 和 _prev 指针指向自身。_size 初始化为 0,表现链表当前没有元素。
  • initializer_list 构造函数:允许使用初始化列表创建链表,通过 push_back 方法逐个插入元素。
  • 拷贝构造函数:复制另一个链表的内容,同样调用 empty_init() 初始化新链表,然后逐个插入元素。
  • 赋值运算符重载:通过传值方式吸收参数,利用拷贝构造函数创建一个暂时对象 lt,然后互换内部数据,确保赋值操作的安全性和高效性。
  • 析构函数:资源管理,调用 clear() 清除所有节点,释放它们的内存,末了释放链表头节点的内存,避免内存泄漏。
迭代器类中的运算符重载函数 

  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.                 Node* _node;
  7.                 //带参构造
  8.                 list_iterator(Node* node)
  9.                         :_node(node)
  10.                 {}
  11.                 Ref operator*()
  12.                 {
  13.                         return _node->_data;
  14.                 }
  15.                 Ptr operator->()
  16.                 {
  17.                         return &_node->_data;
  18.                 }
  19.                 Self& operator++()
  20.                 {
  21.                         node = _node->_next;
  22.                         return *this;
  23.                 }
  24.                 Self operator++(int)
  25.                 {
  26.                         Self tmp(*this);
  27.                         _node =  _node->_next;
  28.                         return tmp;
  29.                 }
  30.                 Self& operator--()
  31.                 {
  32.                         node = _node->_prev;
  33.                         return *this;
  34.                 }
  35.                 Self operator--(int)
  36.                 {
  37.                         Self tmp(*this);
  38.                         _node = _node->_prev;
  39.                         return tmp;
  40.                 }
  41.                 bool operator!=(const Self& node) const
  42.                 {
  43.                         return _node != node;
  44.                 }
  45.                 bool operator==(const Self& node) const
  46.                 {
  47.                         return _node == node;
  48.                 }
  49.         };
复制代码
list类中的常用函数:

  1. //迭代器
  2. typedef list_iterator<T, T&, T*> iterator;
  3. typedef list_iterator<T, const T&, const T*> const_iterator;
  4. iterator begin()
  5. {
  6.         return _head->_next;
  7. }
  8. iterator end()
  9. {
  10.         return _head;
  11. }
  12. const_iterator begin() const
  13. {
  14.         return _head->_next;
  15. }
  16. const_iterator end() const
  17. {
  18.         return _head;
  19. }
  20. //insert
  21. iterator insert(iterator pos, const T& x)
  22. {
  23.         Node* cur = pos._node;
  24.         Node* prev = cur->_prev;
  25.         Node* newnode = new Node(x);
  26.         newnode->_next = cur;
  27.         newnode->_prev = prev;
  28.         prev->_next = newnode;
  29.         cur->_prev = newnode;
  30.         ++_size;
  31.         return newnode;
  32. }
  33. void push_back(const T& x)
  34. {
  35.         insert(end(), x);
  36. }
  37. void push_front(const T& x)
  38. {
  39.         insert(begin(), x);
  40. }
  41. //erase
  42. iterator erase(iterator pos)
  43. {
  44.         Node* cur = pos._node;
  45.         Node* prev = cur->_prev;
  46.         Node* next = cur->_next;
  47.         prev->_next = next;
  48.         next->_prev = prev;
  49.         delete cur;
  50.         --_size;
  51.         return next;
  52. }
  53. void pop_back()
  54. {
  55.         erase(--end());
  56. }
  57. void pop_front()
  58. {
  59.         erase(begin());
  60. }
  61. //size
  62. size_t size() const
  63. {
  64.         return _size;
  65. }
  66. //empty
  67. bool empty()
  68. {
  69.         return _size == 0;
  70. }
  71.         private:
  72.                 Node* _head;
  73.                 size_t _size;
复制代码
        这段代码实现了双向链表的迭代器、插入、删除、大小和空链表查抄的功能,确保链表可以或许灵活操作和高效管理节点。


  • iterator:可修改元素的迭代器,指向链表中的节点。
  • const_iterator:只读迭代器,确保不修改链表中的元素。
  • begin():返回指向链表第一个有效元素的迭代器(头节点的下一个节点)。
  • end():返回指向链表头节点的迭代器,表现链表的结束。
  • insert(pos, x):在指定位置 pos 插入新节点 x。
  • push_back(x):在链表末了插入元素 x,通过 insert(end(), x) 实现。
  • push_front(x):在链表开头插入元素 x,通过 insert(begin(), x) 实现。
  • erase(pos):删除指定位置的节点。
  • pop_back():删除链表末了的元素,通过 erase(--end()) 实现。
  • pop_front():删除链表开头的元素,通过 erase(begin()) 实现。
  • size():返回链表中元素的数目。
  • empty():查抄链表是否为空,返回布尔值。

完整代码展示:

  1. #pragma once
  2. #include<iostream>
  3. #include<list>
  4. #include<assert.h>
  5. #include<algorithm>
  6. using namespace std;
  7. namespace zy
  8. {
  9.         template<class T>
  10.         struct list_node
  11.         {
  12.                 T _data;
  13.                 list_node<T>* _next;
  14.                 list_node<T>* _prev;
  15.                 //默认构造
  16.                 list_node(const T& data = T())
  17.                         :_data(data)
  18.                         ,_next(nullptr)
  19.                         ,_prev(nullptr)
  20.                 {}
  21.         };
  22.        
  23.         //template<class T>
  24.         //struct list_const_iterator
  25.         //{
  26.         //        typedef list_node<T> Node;
  27.         //        typedef list_const_iterator<T> Self;
  28.         //       
  29.         //        Node* _node;
  30.         //        list_const_iterator(Node* node)
  31.         //                :_node(node)
  32.         //        {}
  33.         //        // operator*
  34.         //        const T& operator*()
  35.         //        {
  36.         //                return _node->_data;
  37.         //        }
  38.         //        // operator->
  39.         //        const T* operator->()
  40.         //        {
  41.         //                return &_node->_data;
  42.         //        }
  43.         //        // operator++
  44.         //        Self& operator++()
  45.         //        {
  46.         //                _node = _node->_next;
  47.         //                return *this;
  48.         //        }
  49.         //        Self operator++(int)
  50.         //        {
  51.         //                Self tmp(*this);
  52.         //                _node = _node->_next;
  53.         //                return tmp;
  54.         //        }
  55.         //        // operator--
  56.         //        Self& operator--()
  57.         //        {
  58.         //                _node = _node->_prev;
  59.         //                return *this;
  60.         //        }
  61.         //        Self operator--(int)
  62.         //        {
  63.         //                Self tmp(*this);
  64.         //                _node = _node->_prev;
  65.         //                return tmp;
  66.         //        }
  67.         //        // operator!=
  68.         //        bool operator!=(const Self& s)
  69.         //        {
  70.         //                return _node != s._node;
  71.         //        }
  72.         //};
  73.         template<class T, class Ref, class Ptr>
  74.         struct list_iterator
  75.         {
  76.                 typedef list_node<T> Node;
  77.                 typedef list_iterator<T, Ref, Ptr> Self;
  78.                 Node* _node;
  79.                 list_iterator(Node* node)
  80.                         :_node(node)
  81.                 {}
  82.                 // operator*
  83.                 Ref operator*()
  84.                 {
  85.                         return _node->_data;
  86.                 }
  87.                 // operator->
  88.                 Ptr operator->()
  89.                 {
  90.                         return &_node->_data;
  91.                 }
  92.                 // operator++
  93.                 Self& operator++()
  94.                 {
  95.                         _node = _node->_next;
  96.                         return *this;
  97.                 }
  98.                 Self operator++(int)
  99.                 {
  100.                         Self tmp(*this);
  101.                         _node = _node->_next;
  102.                         return tmp;
  103.                 }
  104.                 // operator--
  105.                 Self& operator--()
  106.                 {
  107.                         _node = _node->_prev;
  108.                         return *this;
  109.                 }
  110.                 Self operator--(int)
  111.                 {
  112.                         Self tmp(*this);
  113.                         _node = _node->_prev;
  114.                         return tmp;
  115.                 }
  116.                 // operator!=
  117.                 bool operator!=(const Self& s) const
  118.                 {
  119.                         return _node != s._node;
  120.                 }
  121.                 // operator==
  122.                 bool operator==(const Self& s) const
  123.                 {
  124.                         return _node == s._node;
  125.                 }
  126.         };
  127.         template<class T>
  128.         class list
  129.         {
  130.                 //typedef 也受访问限定符的限制
  131.                 typedef list_node<T> Node;
  132.         public:
  133.                 void empty_init()
  134.                 {
  135.                         //设置哨兵位
  136.                         _head = new Node();
  137.                         _head->_next = _head;
  138.                         _head->_prev = _head;
  139.                 }
  140.                 list()
  141.                 {
  142.                         empty_init();
  143.                 }
  144.                 list(initializer_list<T>& li)
  145.                 {
  146.                         empty_init();
  147.                         for (auto& e : li)
  148.                         {
  149.                                 push_back(e);
  150.                         }
  151.                 }
  152.                 //拷贝构造
  153.                 list(const list<T>& lt)
  154.                 {
  155.                         empty_init();
  156.                         auto it = lt.begin();
  157.                         while (it != lt.end())
  158.                         {
  159.                                 push_back(it);
  160.                                 ++it;
  161.                         }
  162.                 }
  163.                 //swap
  164.                 void swap(list<T>& lt)
  165.                 {
  166.                         std::swap(_head, lt._head);
  167.                         std::swap(_size, lt._size);
  168.                 }
  169.                 //赋值重载
  170.                 list<T>& operator=(list<T> lt)
  171.                 {
  172.                         swap(lt);
  173.                         return *this;
  174.                 }
  175.                 //clear
  176.                 void clear()
  177.                 {
  178.                         auto it = begin();
  179.                         while (it != end())
  180.                         {
  181.                                 it = erase(it);
  182.                         }
  183.                 }
  184.                 //析构
  185.                 ~list()
  186.                 {
  187.                         clear();
  188.                         delete _head;
  189.                         _head = nullptr;
  190.                 }
  191.                 //iterator
  192.                 typedef list_iterator<T, T&, T*> iterator;
  193.                 typedef list_iterator<T, const T&, const T*> const_iterator;
  194.                 iterator begin()
  195.                 {
  196.                         //使用迭代器类创建一个头结点的迭代器
  197.                         /*iterator it(_head->_next);
  198.                         return it;*/
  199.                         //返回匿名对象
  200.                         //return iterator(_head->_next);
  201.                         //隐式类型转换,返回的节点由iterator类型接收
  202.                         return _head->_next;
  203.                 }
  204.                 iterator end()
  205.                 {
  206.                         return _head;
  207.                 }
  208.                 const_iterator begin() const
  209.                 {
  210.                         return _head->_next;
  211.                 }
  212.                 const_iterator end() const
  213.                 {
  214.                         return _head;
  215.                 }
  216.                 //  push_back
  217.                 void push_back(const T& x)
  218.                 {
  219.                         //new新节点
  220.                         //Node* newnode = new Node(x);
  221.                         //Node* tail = _head->_prev;
  222.                         //插入新节点
  223.                         //newnode->_prev = tail;
  224.                         //newnode->_next = _head;
  225.                         //tail->_next = newnode;
  226.                         //_head->_prev = newnode;
  227.                         //++_size;
  228.                         //直接复用insert
  229.                         insert(end(), x);
  230.                 }
  231.                 //push_front
  232.                 void push_front(const T& x)
  233.                 {
  234.                         //直接复用insert
  235.                         insert(begin(), x);
  236.                 }
  237.                
  238.                 // insert
  239.                 iterator insert(iterator pos, const T& x)
  240.                 {
  241.                         //        找到插入位置
  242.                         Node* cur = pos._node;
  243.                         Node* prev = cur->_prev;
  244.                         //new出新节点
  245.                         Node* newnode = new Node(x);
  246.                         //进行连接
  247.                         newnode->_prev = prev;
  248.                         newnode->_next = cur;
  249.                         cur->_prev = newnode;
  250.                         prev->_next = newnode;
  251.                         //++size
  252.                         ++_size;
  253.                         return newnode;
  254.                 }
  255.                 //pop_front
  256.                 void pop_front()
  257.                 {
  258.                         erase(begin());
  259.                 }
  260.                 //pop_back
  261.                 void pop_back()
  262.                 {
  263.                         erase(--end());
  264.                 }
  265.                 //erase
  266.                 iterator erase(iterator pos)
  267.                 {
  268.                         //不能释放哨兵位
  269.                         assert(pos != end());
  270.                        
  271.                         Node* node = pos._node;
  272.                         Node* prev = node->_prev;
  273.                         Node* next = node->_next;
  274.                         prev->_next = next;
  275.                         next->_prev = prev;
  276.                         delete node;
  277.                         --_size;
  278.                         //隐式类型转换为iterator
  279.                         return next;
  280.                 }
  281.                 // size
  282.                 size_t size() const
  283.                 {
  284.                         return _size;
  285.                 }
  286.                 //empty
  287.                 bool empty()
  288.                 {
  289.                         return _size == 0;
  290.                 }
  291.         private:
  292.                 Node* _head;
  293.                 size_t _size = 0;
  294.         };
  295.         // 打印函数
  296.         template<class Container>
  297.         void print_container(const Container& con)
  298.         {
  299.                 //auto it = con.begin();
  300.                 typename Container::const_iterator it = con.begin();
  301.                 while (it != con.end())
  302.                 {
  303.                         cout << *it << " ";
  304.                         ++it;
  305.                 }
  306.                 cout << endl;
  307.         }
  308.         void  list_test1()
  309.         {
  310.                 list<int> lt;
  311.                 lt.push_back(1);
  312.                 lt.push_back(2);
  313.                 lt.push_back(3);
  314.                 lt.push_back(4);
  315.                 lt.push_back(5);
  316.                 list<int>::iterator it = lt.begin();
  317.                 while (it != lt.end())
  318.                 {
  319.                         cout << *it << " ";
  320.                         ++it;
  321.                 }
  322.                 cout << endl;
  323.                 print_container(lt);
  324.         }
  325. }
复制代码

list 与 vector 的对比

vectorlist底层结构动态次序表,一段连续的空间带头节点的双向循环链表随机访问支持,访问单个的效率为O(1)不支持,访问单个元素的效率为O(1)插入和删除在恣意位置插入和删除效率低,必要搬移元素,时间复杂度为O(N),插入时有可能必要增容,增容:开发新空间拷贝元素,释放旧空间,导致效率更低。恣意位置插入和删除效率高,不必要搬移元素,时间复杂度为O(1)。迭代器原生态指针独立封装的节点指针迭代器失效插入删除都可能会导致迭代器失效。只有删除会导致迭代器失效,但是不会影响反面的位置的迭代器。空间利用率空间利用率高,缓存利用率高空间利用率低,缓存利用率低使用场景必要高效储存,支持随机访问,不关心插入和删除效率大量插入删除,不关心随机访问

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表