cpp_list

【本节目标】
- list的介绍及使用
- list的深度剖析及模拟实现
- list与vector的对比
🤔🤔思考:为什么会有
list?
首先补充vector存在的的缺点
vector缺点:
- 头部和中部的插入删除效率低。O(N),因为需要挪动数据。
- 插入数据空间不够需要增容。增容需要开新空间、拷贝数据、释放旧空间,会付出很大的代价。
优点:
- 支持下标的随机访问。间接的就很好的支持
排序、二分查找、堆算法等等。
注意:
list出现就是为了解决vector的缺陷
优点
- 1ist头部、中间插入不再需要挪动数据,效率高。0(1)
- list插入数据是新增节点,不需要增容。
缺点
- 不支持随机访问。
所以实际使用中vector和list是相辅相成的两个容器
1. list的介绍及使用
1.1 list的介绍
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
1.2 list的使用
list中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已达到可扩展的能力。
1.2.1 list的增删
#include<list>void test_list1()
{//带头双向循环链表list<int> l;//尾插l.push_back(1);l.push_back(2);l.push_back(3);l.push_back(4);//头插l.push_front(0);l.push_front(-1);l.push_front(-2);list<int>::iterator it = l.begin();while (it != l.end()){cout << *it << " ";++it;}cout << endl;//尾删l.pop_back();//头删l.pop_front();for (auto e : l){cout << e << " ";}cout << endl;//反向遍历list容器list<int>::reverse_iterator rit = l.rbegin();while (rit != l.rend()){cout << *rit << " ";++rit;}cout << endl;}int main()
{test_list1();return 0;
}

只读的list打印工具函数
void print_list(const list<int>& l)
{list<int>::const_iterator it = l.begin();while (it != l.end()){//*it = l;不可以修改cout << *it << " ";++it;}cout << endl;
}
-
安全性:确保打印函数不会意外修改数据
-
明确性:通过函数签名表明这是只读操作
-
效率:使用引用避免拷贝整个list
1.2.2 list的简单调用
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<list>using namespace std;void print_list(const list<int>& l)
{list<int>::const_iterator it = l.begin();while (it != l.end()){//*it = l;不可以修改cout << *it << " ";++it;}cout << endl;
}
void test_list1()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);list<int>::iterator it1 = lt1.begin();while (it1 != lt1.end()){cout << *it1 << " ";++it1;}cout << endl;//拷贝构造list<int> lt2(lt1);print_list(lt2);//赋值list<int> lt3;lt3.push_back(10);lt3.push_back(20);lt3.push_back(30);lt3.push_back(40);lt1 = lt3;//只要一个容器支持迭代器,就可以使用范围for的操作for (auto e : lt1){cout << e << " ";}cout << endl;//reverse 逆置//reserve 保留list<int>::reverse_iterator rit1 = lt1.rbegin();while (rit1 != lt1.rend()){cout << *rit1 << " ";++rit1;}cout << endl;}
int main()
{test_list1();return 0;
}

1.2.3 list中任意位置的插入删除
//任意位置的插入删除
void print_list(const list<int>& l)
{list<int>::const_iterator it = l.begin();while (it != l.end()){//*it = l;不可以修改cout << *it << " ";++it;}cout << endl;
}
void test_list3()
{list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_back(5);lt.push_back(6);print_list(lt);//那么想在3的前面插入一个30怎么办呢//list是不支持lt.insert(lt.begin()+2,30)的list<int>::iterator pos = find(lt.begin(), lt.end(), 3);if (pos != lt.end())//为什么要这个判断?危险!如果pos是end(),这是未定义行为!{lt.insert(pos, 30);//在3前面插入30//这里insert以后pos失效了吗}print_list(lt);}

🤔🤔思考:insert以后pos失效了吗
list的insert操作不会使迭代器失效
插入后,原来的pos仍然指向原来的元素(值为3的节点)
补充:不同容器的迭代器失效规则
| 容器 | insert操作 | erase操作 |
|---|---|---|
| 容器 | insert操作 | erase操作 |
| list | 不会失效 | 被删除的迭代器失效,其他不受影响 |
| vector | 插入点及之后的所有迭代器可能失效 | 被删除点及之后的所有迭代器失效 |
关键点:list的节点式存储结构使得插入删除操作不会影响其他迭代器的有效性,这也是选择list而不是vector的一个重要考虑因素。
1.2.4 list的迭代器失效

修改如下:
void test_list5()
{list<int> lt;lt.push_back(3);lt.push_back(2);lt.push_back(1);lt.push_back(5);lt.push_back(4);lt.push_back(6);//假设要删除偶数list<int>::iterator it = lt.begin();while (it != lt.end()){if (*it % 2 == 0){it = lt.erase(it);//用it来接收这个位置}else{++it;}}print_list(lt);}
为什么会失效?
当调用 lt.erase(it) 时:
-
被删除的节点从链表中移除
-
指向被删除节点的迭代器
it变为无效 -
对无效迭代器进行任何操作(包括 ++)都是未定义行为
迭代器失效的总结:
1、vector的iterator(指的是迭代器),insert、erase都会导致失效->原因:vector是连续存储的数组
2、list的iterator,erase会导致失效->原因:list是链表结构
2. list的模拟实现
2.1 模拟实现list
#pragma once
#include <iostream>
namespace tree
{template<class T>struct __list_node{__list_node<T>* _next;__list_node<T>* _prev;T _data;__list_node(const T& x=T()):_data(x), _next(nullptr), _prev(nullptr){ }};//构造迭代器template<class T>struct __list_iterator{typedef __list_node<T> Node;Node* _node;__list_iterator(Node* node):_node(node){ }//*itT& operator*(){return _node->_data;}//++it__list_iterator<T>& operator--(){_node = _node->_prev;return *this;}//it++__list_iterator<T> operator++(int){__list_iterator<T> tmp = *this;_node = _node->_next;return tmp;}//--it__list_iterator<T>& operator++(){_node = _node->_next;return *this;}//it--__list_iterator<T> operator--(int){__list_iterator<T> tmp = *this;_node = _node->_prev;return tmp;}//it!=end()bool operator!=(const __list_iterator<T>& it){return _node != it._node;}};template<class T>class list{typedef __list_node<T> Node;public:typedef __list_iterator<T> iterator;iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}//带头双向循环链表list(){_head = new Node;_head->_next = _head;_head->_prev = _head;}void push_back(const T& x){Node* tail = _head->_prev;Node* newnode = new Node(x);tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}private:Node* _head;};void test_list1(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list<int>::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;}
}
双向循环链表分析:

结果:

3. list与vector的对比
vector与list都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不同,其主要不同如下:

| vector | list | |
|---|---|---|
| 底层结构 | 动态顺序表,一段连续空间 | 带头结点的双向循环链表 |
| 随机访问 | 支持随机访问,访问某个元素效率O(1) | 不支持随机访问,访问某个元素效率O(N) |
| 插入和删除 | 任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低 | 任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1) |
| 空间利用率 | 底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高 | 底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低 |
| 迭代器 | 原生态指针 | 对原生态指针(节点指针)进行封装 |
| 迭代器失效 | 在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效 | 插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响 |
| 使用场景 | 需要高效存储,支持随机访问,不关心插入删除效率 | 大量插入和删除操作,不关心随机访问 |

