【C++】STL之list深度剖析及模拟实现

目录

前言

一、list 的使用

 1、构造函数

2、迭代器

3、增删查改

4、其他函数使用

二、list 的模拟实现

 1、节点的创建

 2、push_back 和 push_front

 3、普通迭代器

 4、const 迭代器

 5、增删查改(insert、erase、pop_back、pop_front)

 6、构造函数和析构函数

  6.1、默认构造

  6.2、构造 n 个 val 的对象

  6.3、拷贝构造

  6.4、迭代器区间构造

  6.5、 赋值运算符重载

  6.6、析构函数

三、list 模拟实现源代码

四、list 的迭代器失效

五、list 和 vector的对比


前言

  1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
  3. list 与 forward_list 非常相似:最主要的不同在于 forward_list 是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list 通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问.

一、list 的使用

 1、构造函数

构造函数接口说明
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
int main()
{// 默认构造list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);// 拷贝构造list<int> lt2(lt);// 构造 n 个节点list<int> lt3(5, 1);// 迭代器区间构造list<int> lt4(lt.begin(), lt.end());return 0;
}

2、迭代器

函数声明接口说明
begin + end返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin + rend返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置
int main()
{int a[] = { 1,2,3,4,5,6,7,8,9 };list<int> lt(a, a + 9);auto it = lt.begin();while (it != lt.end()){cout << *it << " ";it++;}cout << endl;return 0;
}

迭代器一般是用来遍历和查找的; 

而反向迭代器的使用是类似的,只不过调用的函数换成了 rbegin 和 rend 。

注意:反向迭代器的迭代使用的也是++。但迭代器区间一样是[rbegin, rend);

3、增删查改

函数声明接口说明
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 中的有效元素
int main()
{vector<int> v = { 1,2,3,4,5,6,7,8,9 };list<int> lt(v.begin(), v.end());for (auto e : lt) cout << e << " ";cout << endl;lt.push_front(10);lt.push_back(20);for (auto e : lt) cout << e << " ";cout << endl;lt.pop_front();lt.pop_back();for (auto e : lt) cout << e << " ";cout << endl;auto pos = find(lt.begin(), lt.end(), 5);lt.insert(pos, 50);for (auto e : lt) cout << e << " ";cout << endl;pos = find(lt.begin(), lt.end(), 8);lt.erase(pos);for (auto e : lt) cout << e << " ";cout << endl;return 0;
}

4、其他函数使用

函数声明接口说明
empty检测 list 是否为空,是返回 true ,否则返回 false
size返回 list 中有效节点的个数
front返回 list 的第一个节点中值的引用
back返回 list 的最后一个节点中值的引用

二、list 的模拟实现

 1、节点的创建

template<class T>
struct list_node//节点
{list_node<T>* _next;list_node<T>* _prev;T _data;// 构造函数list_node(const T& x = T()):_next(nullptr), _prev(nullptr), _data(x){}
};

   由于节点存储的数据可能是任意类型,所以我们需要将将节点定义为模板类。这里我们需要写一个给缺省值的默认构造函数,便于之后在主类中new一个新节点时直接初始化,同时将两个指针置为空,将数据写入数据域中。

 2、push_back 和 push_front

class list 
{
public:typedef list_node<T> node;private:node* _head;
}
//尾插
void push_back(const T& x) const
{node* new_node = new node(x);node* tail = _head->_prev;//链接节点之间的关系tail->_next = new_node;new_node->_prev = tail;new_node->_next = _head;_head->_prev = new_node;
}
//头插
void push_front(const T& x)
{node* head = _head->_next;node* new_node = new node(x);_head->_next = new_node;new_node->_prev = _head;new_node->_next = head;head->_prev = new_node;
}

 这里模拟的头插和尾插也很简单,因为和我们之前在数据结构时候的双向循环链表是一样的,只需要找到头或者尾,然后链接四个节点间的关系即可。

 3、普通迭代器

注意:list 的迭代器是自定义类型,不是原生指针node*。

迭代器为自定义类型,其中*,++等都是通过运算符重载来完成的。

所以我们需要重载的符号:*,->,前置++,后置++,前置--,后置--,!=,==

template<class T>
struct __list_iterator
{typedef list_node<T> node;typedef __list_iterator<T> self;node* _node;//构造函数__list_iterator(node* n):_node(n){}//重载*运算符T& operator*(){return _node->_val;}T* operator->(){return &_node->_data;}//重载前置++运算符self& operator++(){_node = _node->_next;return *this;}//重载后置++运算符self operator++(int){self tmp(*this);_node = _node->_next;return tmp;}//重载前置--运算符self& operator--(){_node = _node->_prev;return *this;}//重载后置--运算符self operator--(int){self tmp(*this);_node = _node->_prev;return tmp;}//重载!=运算符bool operator!=(const self& s){return _node != s._node;}//重载==运算符bool operator==(const self& s){return _node == s._node;}
};

 此处我实现了一个简单的正向迭代器,使用一个模板参数T表示类型。

 当普通迭代器封装好了之后,我们需要在list类中来实现它的 begin() 和 end() 方法。由于迭代器的名字一般都是 iterator,而且对于范围for来说,也只能通过 iterator 来转换为迭代器进行遍历。所以这里我们将其typedef为iterator。

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);}
private:node* _head;
};

 4、const 迭代器

  const迭代器与普通迭代器的区别在于const迭代器指向的内容是不能修改的,但是它的指向是可以修改的。

template<class T>
class list//链表
{typedef list_node<T> node;
public:typedef __list_const_iterator<T> const_iterator;const_iterator begin(){return const_iterator(_head->_next);}const_iterator end(){return const_iterator(_head);}
private:node* _head;
};

  我们最好的做法就是在__list_iterator 的类模板中的添加两个模板参数,然后再 list 类中 typedef 两份分别将第二个参数分别改成 T& 和 const T& 的类型,本质上就是让编译器根据传入的 Ref 的不同来自动示例化出 const 迭代器类,而我们还需要重载一个->运算符,因为list中可能存储的是自定义类型,这个自定义类型如果要是有多个成员变量的话,我们就需要使用->来解引用访问成员变量,同样还是要区分普通迭代器和const 迭代器,所以就增加了另一个模版参数 Ptr。具体的解决做法如下:

template<class T, class Ref, class Ptr>
struct __list_iterator
{typedef list_node<T> node;typedef __list_iterator<T, Ref, Ptr> self;node* _node;__list_iterator(node* n):_node(n){}Ref operator*()//解引用{return _node->_data;}Ptr operator->(){return &_node->_data;}...
};

然后,最终在链表类中使用如下:

template<class T>
class list//链表
{typedef list_node<T> node;
public:typedef __list_iterator<T, T&, T*> iterator;//普通迭代器typedef __list_iterator<T, const T&, const T*> const_iterator;//const迭代器iterator begin(){return iterator(_head->_next);//匿名对象的返回}const_iterator begin() const{return const_iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator end() const{return const_iterator(_head);}
private:node* _head;
};

 5、增删查改(insert、erase、pop_back、pop_front)

// 指定位置插入
void insert(iterator pos, const T& x)
{node* cur = pos._node;node* prev = cur->_prev;node* new_node = new node(x);prev->_next = new_node;new_node->_prev = prev;new_node->_next = cur;cur->_prev = new_node;
}
// 指定位置删除
iterator erase(iterator pos)
{assert(pos != end());node* prev = pos._node->_prev;node* next = pos._node->_next;prev->_next = next;next->_prev = prev;delete pos._node;return iterator(next);
}
// 尾删
void pop_back()
{erase(--end());
}
// 头删
void pop_front()
{erase(begin());
}

 6、构造函数和析构函数

  6.1、默认构造

  由于后面会频繁对空进行初始化,所以在这里对它进行了封装,方便后面的调用。

void empty_init()//空初始化
{_head = new node;_head->_next = _head;_head->_prev = _head;
}
list()
{empty_init();
}

  6.2、构造 n 个 val 的对象

//用n个val构造对象
list(int n, const T& val = T())
{empty_init();for (int i = 0; i < n; i++){push_back(val);}
}

  6.3、拷贝构造

//拷贝构造传统写法
list(const list<T>& lt)
{empty_init();for (auto e : lt){push_back(e);}
}
//拷贝构造现代写法
list(const list<T>& lt)
{empty_init();list<T> tmp(lt.begin(), lt.end());swap(tmp);
}

  6.4、迭代器区间构造

template <class Iterator>
list(Iterator first, Iterator last)
{empty_init();while (first != last){push_back(*first);++first;}
}

  6.5、 赋值运算符重载

//赋值运算符重载
list<T>& operator=(list<T> lt)//注意这里不能用引用
{swap(lt);return *this;
}

  6.6、析构函数

//要全部清理掉
~list()
{clear();delete _head;_head = nullptr;
}
//不释放头结点
void clear()
{iterator it = begin();while (it != end()){it = erase(it);//这样也可以//erase(it++);}
}

三、list 模拟实现源代码

template<class T>
struct list_node//节点
{list_node<T>* _next;list_node<T>* _prev;T _data;list_node(const T& x = T()):_next(nullptr), _prev(nullptr), _data(x){}
};
template<class T, class Ref, class Ptr>
struct __list_iterator
{typedef list_node<T> node;typedef __list_iterator<T, Ref, Ptr> self;node* _node;__list_iterator(node* n):_node(n){}Ref operator*()//解引用{return _node->_data;}Ptr operator->(){return &_node->_data;}//前置++self& operator++(){_node = _node->_next;return *this;}//后置++self operator++(int){self tmp(*this);_node = _node->_next;return tmp;}//前置--self& operator--(){_node = _node->_prev;return *this;}//后置--self operator--(int){self tmp(*this);_node = _node->_prev;return tmp;}bool operator!=(const self& s){return _node != s._node;}bool operator==(const self& s){return _node == s._node;}
};
template<class T>
class list//链表
{typedef list_node<T> node;
public:typedef __list_iterator<T, T&, T*> iterator;//普通迭代器typedef __list_iterator<T, const T&, const T*> const_iterator;//const迭代器iterator begin(){return iterator(_head->_next);//匿名对象的返回}const_iterator begin() const{return const_iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator end() const{return const_iterator(_head);}void empty_init()//空初始化{_head = new node;_head->_next = _head;_head->_prev = _head;}list(){empty_init();}//迭代器区间构造template <class Iterator>list(Iterator first, Iterator last){empty_init();while (first != last){push_back(*first);//push_back使用的前提是要有哨兵位的头结点++first;}}// 交换函数void swap(list<T>& tmp){std::swap(_head, tmp._head);}//现代拷贝构造list(const list<T>& lt){list<T> tmp(lt.begin(), lt.end());swap(tmp);}//现代赋值写法list<T>& operator=(list<T> lt){swap(lt);return *this;}~list()//要全部清理掉{clear();delete _head;_head = nullptr;}void clear()//不释放头结点{iterator it = begin();while (it != end()){it = erase(it);//这样也可以//erase(it++);}}void insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* new_node = new node(x);prev->_next = new_node;new_node->_prev = prev;new_node->_next = cur;cur->_prev = new_node;}iterator erase(iterator pos){assert(pos != end());node* prev = pos._node->_prev;node* next = pos._node->_next;prev->_next = next;next->_prev = prev;delete pos._node;return iterator(next);}//尾插void push_back(const T& x) const{//node* new_node = new node(x);//node* tail = _head->_prev;链接节点之间的关系//tail->_next = new_node;//new_node->_prev = tail;//new_node->_next = _head;//_head->_prev = new_node;insert(end(), x);}//头插void push_front(const T& x){//node* head = _head->_next;//node* new_node = new node(x);//_head->_next = new_node;//new_node->_prev = _head;//new_node->_next = head;//head->_prev = new_node;insert(begin(), x);}//尾删void pop_back(){erase(--end());}//头删void pop_front(){erase(begin());}
private:node* _head;
};

四、list 的迭代器失效

  当我们使用 erase 进行删除后,此时指向删除位置的迭代器就失效了,再次使用就会令程序崩溃。

  因此若要多次删除,则需要在使用后利用 erase 的返回值更新迭代器,这样使用才不会出现错误。

int main()
{vector<int> v = { 1, 2,3,5,4,6 };list<int> lt(v.begin(), v.end());list<int>::iterator pos = find(lt.begin(), lt.end(), 3);for (int i = 0; i < 3; i++){pos = lt.erase(pos);   //利用erase的返回值更新迭代器}for (auto e : lt) cout << e << " ";cout << endl;return 0;
}

五、list 和 vector的对比

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


本文要是有不足的地方,欢迎大家在下面评论,我会在第一时间更正。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/140776.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Qt QCustomPlot介绍

介绍 主要介绍qcustomplot及其用法 最新版本:QCustomPlot Patch Release 2.1.1//November 6, 2022 下载:https://www.qcustomplot.com/index.php/download 官网:https://www.qcustomplot.com/index.php 简单使用 mainwindow.h /**************************************…

【pytest】 参数化@pytest.mark.parametrize

1.创建 test_parametrize.py 通过 pytest.mark.parametrize 方法设置参数 import pytestimport math#pytest参数化 pytest.mark.parametrize("base,exponent,expected", # 参数变量名称# 每个元组都是一条测试用例测试数据[(2,2,4),(3,3,9),(1,9,1),(0,9,0)],i…

R语言风险价值:ARIMA,GARCH,Delta-normal法滚动估计VaR(Value at Risk)和回测分析股票数据...

全文链接&#xff1a;http://tecdat.cn/?p24492 此分析的目的是构建一个过程&#xff0c;以在给定时变波动性的情况下正确估计风险价值。风险价值被广泛用于衡量金融机构的市场风险。我们的时间序列数据包括 1258 天的股票收益&#xff08;点击文末“阅读原文”获取完整代码数…

Java————网络编程

一 、网络编程基础 1. 为什么需要网络编程 用户在浏览器中&#xff0c;打开在线视频网站&#xff0c; 如优酷看视频&#xff0c;实质是通过网络&#xff0c; 获取到网络上的一个视频资源。 与本地打开视频文件类似&#xff0c;只是视频文件这个资源的来源是网络。 相比本地资…

汽车电子——产品标准规范汇总和梳理(车载网络)

文章目录 前言 一、菊花链 二、K Line 三、L Line 四、RS485 五、LIN 六、CAN 七、FlexRay 八、MOST 九、Bluetooth 十、LAN 十一、移动网络 十二、实施和测试 总结 前言 见《汽车电子——产品标准规范汇总和梳理》 一、菊花链 暂无统一的正式标准。 菊花链通信&…

Linux查看系统信息

# 查看操作系统的详细信息 uname -a# 查看已安装的Linux发行版信息 cat /etc/os-release# 查看Linux Standard Base (LSB)的信息 lsb_release -a# 查看主机的信息 hostnamectl# 查看文件系统的磁盘空间使用情况 df -h# 查看系统内存的使用情况 free -h# 查看网络接口的信息 ifc…

[React] react-hooks如何使用

react-hooks思想和初衷&#xff0c;也是把组件&#xff0c;颗粒化&#xff0c;单元化&#xff0c;形成独立的渲染环境&#xff0c;减少渲染次数&#xff0c;优化性能。 文章目录 1.为什么要使用hooks2.如何使用hooks2.1 useState2.2 useEffect2.3 useLayoutEffect2.4 useRef2.5…

【网络编程】TCP Socket编程

TCP Socket编程 1. ServerSocket2. Socket3. TCP的长短连接4. Socket 通信模型5. 代码示例&#xff1a;TCP 回显服务器 流套接字&#xff1a; 使用传输层TCP协议 TCP: 即Transmission Control Protocol&#xff08;传输控制协议&#xff09;&#xff0c;传输层协议。 TCP的特点…

【计算机网络】IP协议(下)

文章目录 1. 特殊的IP地址2. IP地址的数量限制3. 私有IP地址和公网IP地址私有IP为什么不能出现在公网上&#xff1f;解决方案——NAT技术的使用 4. 路由5. IP分片问题为什么要进行切片&#xff1f;如何做的分片和组装&#xff1f;16位标识3位标志13位片偏移例子 细节问题如何区…

基于springboot地方废物回收机构管理系统springboot11

大家好✌&#xff01;我是CZ淡陌。一名专注以理论为基础实战为主的技术博主&#xff0c;将再这里为大家分享优质的实战项目&#xff0c;本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#xff0c;希望你能有所收获&#xff0c;少走一些弯路…

智慧农业农场小程序源码 智慧农场系统源码

智慧农业农场小程序源码 智慧农场系统源码 一、 智慧农场系统的组成 智慧农场系统一般包括传感器、控制器、数据采集与处理平台、应用软件等组成部分。其中, 传感器主要用于采集土壤温度、湿度、光照强度等环境参数,以及作物生长状态、水肥情况等生产信息。控制器则根据传感器…

GLTF编辑器的另一个作用

1、GLB模型介绍 GLB&#xff08;GLTF Binary&#xff09;是一种用于表示三维模型和场景的文件格式。GLTF是"GL Transmission Format"的缩写&#xff0c;是一种开放的、跨平台的标准&#xff0c;旨在在各种3D图形应用程序和引擎之间进行交换和共享。 GLB文件是GLTF文件…

PyCharm 手动下载插件

插件模块一直加载失败&#xff0c;报错信息&#xff1a; Marketplace plugins are not loaded. Check the internet connection and refresh. 尝试了以下方法&#xff0c;均告失败&#xff1a; pip 换源Manage Plugin Repositories...HTTP 代理设置...关闭三个防火墙 最后选…

RK3568平台开发系列讲解(工具命令篇)ADB的安装

🚀返回专栏总目录 文章目录 一、ADB介绍二、Windows 下安装 adb 工具沉淀、分享、成长,让自己和他人都能有所收获!😄 一、ADB介绍 adb 全称 Android Debug Bridge,直译过来就是 Android 调试桥,它是一个通用的命令行工具。adb 做为 Android 设备与 PC 端连接的一个桥梁…

MissionPlanner编译过程

环境 windows 10 mission planner 1.3.80 visual studio 2022 git 2.22.0 下载源码 (已配置git和ssh) 从github上克隆源码 git clone gitgithub.com:ArduPilot/MissionPlanner.git进入根目录 cd MissionPlanner在根目录下的ExtLibs文件下是链接的其它github源码&#xff0…

MySQL 高级(进阶) SQL 语句(二) -----存储过程

目录 1 存储过程 1.1 创建存储过程​ 1.2 调用存储过程 1.3 查看存储过程 1.4 存储过程的参数 1.5 修改存储过程 1.6 删除存储过程 2 条件语句 3 循环语句 1 存储过程 存储过程是一组为了完成特定功能的SQL语句集合。 存储过程在使用过程中是将常用或者复杂的工作预…

ClickHouse分布式集群部署

目录 ​编辑 一、环境说明 二、安装部署 2.1 RPM方式安装 2.1.1 安装yum-utils 2.1.2 配置yum repo源 2.1.3 yum install 下载安装clickhouse 2.2 信息配置 2.2.1 配置外网可访问地址 2.2.2 修改存储路径 2.2.2.1 新建存储目录 2.2.2.2 授权 2.2.2.3 修改配置 2.…

单片机第三季-第三课:STM32开发板原理图、配置、浮点运算单元

目录 1&#xff0c;开发板原理图 2&#xff0c;浮点运算单元&#xff08;FPU&#xff09; 1&#xff0c;开发板原理图 课程视频比较早&#xff0c;介绍了三款开发板。观看视频时用的开发板说和51单片机共板的STM32核心板&#xff0c;将51单片机从底座拆下来后&#xff0c;安…

【从0学习Solidity】35. 荷兰拍卖

【从0学习Solidity】35. 荷兰拍卖 博主简介&#xff1a;不写代码没饭吃&#xff0c;一名全栈领域的创作者&#xff0c;专注于研究互联网产品的解决方案和技术。熟悉云原生、微服务架构&#xff0c;分享一些项目实战经验以及前沿技术的见解。关注我们的主页&#xff0c;探索全栈…

黑马JVM总结(十四)

&#xff08;1&#xff09;分代回收_1 Java虚拟机都是结合前面几种算法&#xff0c;让他们协同工作&#xff0c;具体实现是虚拟机里面一个叫做分代的垃圾回收机制&#xff0c;把我们堆内存大的区域划分为两块新生代、老年代 新生代有划分为伊甸园、幸存区Form、幸存区To 为什…