当前位置: 首页 > news >正文

【C++】手撕 list类(包含迭代器)

目录

1,list的介绍及使用

2,list_node

3,list_node()

3,list

4,list()

5,push_back(const T& x)

6,print()

7,_list_iterator

8,operator*()

9,begin()

10,end()

11,operator->()

12,operator++()

13,operator++(int)

14,operator--()

15,operator--(int)

16,operator==(const sefl& s)

17,operator!=(const sefl& s)

18,_list_const_iterator

19,list(iterator first, iterator last)

20,begin()const

21,end()const

22,list(const list& lt)

23,operator=(list lt)

24,insert(iterator pos, const T& x)

25,erase(iterator pos)

26,clear()

27,~list()

28,push_front(const T& x)

29,pop_front()

30,pop_back()

31,源代码

32,总结


1,list的介绍及使用

1,list 是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2,list 的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。

3,list 与 forward_list 非常相似:最主要的不同在于 forward_list 是单链表,只能朝前迭代,已让其更简单高效。

4,与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。

5,与其他序列式容器相比,list 和 forward_list 最大的缺陷是不支持任意位置的随机访问,比如:要访问 list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list 还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

2,list_node

结点的结构体框架

template<class T>struct list_node{list_node<T>* _next;list_node<T>* _prev;T data;};

因为是双向循环链表,需要一个上指针 _prev,下指针 _next,还有数据 data;

3,list_node()

对结点进行初始化

		list_node(const T& x = T()):_next(nullptr), _prev(nullptr), data(x){}

然后还要将其初始化,指针为空,数据为内置类型初始化的值;

3,list

链表结构框架

	template <class T>class list{typedef list_node<T> node;public:private:node* _head;};

链表是带头结点的,所以我们需要一个哨兵位头结点;

4,list()

对链表初始化

		void empty_init(){_head = new node;_head->_next = _head;_head->_prev = _head;}list(){empty_init();}

因为我们是双向循环链表,所以我们的下一个结点和上一个结点都是指向自己的,形成一个环;

5,push_back(const T& x)

尾插

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

我们先找到尾结点(tail),申请一个新结点,然后就插入其中;

6,print()

打印数据

		void print(){node* cur = _head->_next;while (cur != _head){cout << cur->data << " ";cur = cur->_next;}}

哨兵位头结点本身是没有数据的,所以要从下一个结点开始

	void test1(){wxd::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.print();}

也是没有任何问题的

7,_list_iterator

迭代器的框架和初始化

template<class T,class ref,class ptr>struct _list_iterator{typedef list_node<T> node;typedef _list_iterator<T,ref,ptr> sefl;node* _node;_list_iterator(node* n):_node(n){}}

有人会好奇,为什么模板里面有三个参数,现在先不急下面会进行分晓的;

指向结点的迭代器嘛,底层类型就是指针;

初始化也是一样,传来什么就是什么;

8,operator*()

迭代器解引用取值

		ref operator*(){return _node->data;}

ref 其实就是 T&;

9,begin()

找头结点

		iterator begin(){return iterator(_head->_next);}

直接返回构造完后的结果;

10,end()

最后一个结点的下一个位置

		iterator end(){return iterator(_head);}

然后我们就可以试一下迭代器打印了;

	void print_list(const list<int>& lt){list<int>::const_iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;}void test1(){wxd::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);print_list(lt1);}

11,operator->()

迭代器箭头指向取值

		ptr operator->(){return &_node->data;}

返回的是 data 的地址,ptr 是 T* ;

12,operator++()

迭代器前置++

		sefl& operator++(){_node = _node->_next;return *this;}

sefl 是  _list_iterator<T,ref,ptr>;

13,operator++(int)

迭代器后置++

		sefl operator++(int){sefl tmp(*this);_node = _node->_next;return tmp;}

返回的是之前的值,但其实已经改变了;

14,operator--()

迭代器前置 - -

		sefl& operator--(){_node = _node->_prev;return *this;}

15,operator--(int)

迭代器后置 - -

		sefl operator--(int){sefl tmp(*this);_node = _node->_prev;return tmp;}

16,operator==(const sefl& s)

迭代器判断相等

		bool  operator==(const sefl& s){return _node == s._node;}

判断迭代器是否相等比较 _node 就可以了;

17,operator!=(const sefl& s)

判断是否不相等

		bool operator!=(const sefl& s){return _node != s._node;}

18,_list_const_iterator

然后这个是 const 迭代器版本的,这里我就不一个一个写了;

template<class T>struct _list_const_iterator{typedef list_node<T> node;typedef _list_const_iterator<T> sefl;node* _node;_list_const_iterator(node* n):_node(n){}const T& operator*(){return _node->data;}sefl& operator++(){_node = _node->_next;return *this;}sefl operator++(int){sefl tmp(*this);_node = _node->_next;return tmp;}sefl& operator--(){_node = _node->_prev;return *this;}sefl operator--(int){sefl tmp(*this);_node = _node->_prev;return tmp;}bool  operator==(const sefl& s){return _node == s._node;}bool operator!=(const sefl& s){return _node != s._node;}};

其实吧,_list_const_iterator 跟 _list_iterator 就是内部函数参数的返回值不同罢了,我们可以用模板参数来实例化,这样就不用写两个迭代器了;

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;

list 下面这样操作就可以了,普通迭代器模板一个版本,const 迭代器模板内的参数加上const就可以了,等调用的时候编译器会自动匹配的;

19,list(iterator first, iterator last)

迭代器区间构造

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

20,begin()const

const 版本取头结点

		const_iterator begin()const{return const_iterator(_head->_next);}

21,end()const

const 版本取尾结点的下一个位置

		const_iterator end()const{return const_iterator(_head);}

22,list(const list<T>& lt)

拷贝构造

		void swap(list<T>& tmp){std::swap(_head, tmp._head);}list(const list<T>& lt){empty_init();list<T> tmp(lt.begin(), lt.end());swap(tmp);}

先把要拷贝的区间信息构造另一个 list ,然后再与 this 指针的 _head哨兵位头结点进行交换即可;

23,operator=(list<T> lt)

赋值

		list<T>& operator=(list<T> lt){swap(lt);return *this;}

24,insert(iterator pos, const T& x)

插入

		void insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}

定义前一个结点,和本身的结点,然后再进行插入即可;

	void test3(){wxd::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);auto pos = find(lt1.begin(), lt1.end(), 3);lt1.insert(pos, 9);print_list(lt1);}

25,erase(iterator pos)

擦除

		iterator erase(iterator pos){assert(pos != end());node* next = pos._node->_next;node* tail = pos._node->_prev;tail->_next = next;next->_prev = tail;delete pos._node;return iterator(next);}

先断言一下,哨兵位结点是不能擦除的;

然后找到前一个结点,后一个结点,在进行互相绑定;

在释放要删除的空间;

26,clear()

清除

		void clear(){auto it = begin();while (it != end()){it=erase(it);}}

27,~list()

析构函数

		~list(){clear();delete _head;_head = nullptr;}

先清空结点,然后再是否哨兵位头结点置空即可;

28,push_front(const T& x)

头插

		void push_front(const T& x){insert(begin(), x);}

直接用 insert 插入更加方便;

29,pop_front()

头删

		void pop_front(){erase(begin());}

30,pop_back()

尾删

		void pop_back(){erase(_head->_prev);}

31,源代码

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;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> sefl;node* _node;_list_iterator(node* n):_node(n){}ref operator*(){return _node->data;}ptr operator->(){return &_node->data;}sefl& operator++(){_node = _node->_next;return *this;}sefl operator++(int){sefl tmp(*this);_node = _node->_next;return tmp;}sefl& operator--(){_node = _node->_prev;return *this;}sefl operator--(int){sefl tmp(*this);_node = _node->_prev;return tmp;}bool  operator==(const sefl& s){return _node == s._node;}bool operator!=(const sefl& 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;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);++first;}}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;}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 swap(list<T>& tmp){std::swap(_head, tmp._head);}list(const list<T>& lt){empty_init();list<T> tmp(lt.begin(), lt.end());swap(tmp);}list<T>& operator=(list<T> lt){swap(lt);return *this;}void insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}iterator erase(iterator pos){assert(pos != end());node* next = pos._node->_next;node* tail = pos._node->_prev;tail->_next = next;next->_prev = tail;delete pos._node;return iterator(next);}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it != end()){it=erase(it);}}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(_head->_prev);}void pop_front(){erase(begin());}void print(){node* cur = _head->_next;while (cur != _head){cout << cur->data << " ";cur = cur->_next;}}private:node* _head;};

32,总结

我们就先搞一个大概的,其中还有很多分支,比如我们写的是擦除某个数据,其实也可以擦除某个范围,这些就靠大家去摸索,查阅文档了;

list 类的实现就到这里了;

加油!

相关文章:

【C++】手撕 list类(包含迭代器)

目录 1&#xff0c;list的介绍及使用 2&#xff0c;list_node 3&#xff0c;list_node() 3&#xff0c;list 4&#xff0c;list() 5&#xff0c;push_back(const T& x) 6&#xff0c;print() 7&#xff0c;_list_iterator 8&#xff0c;operator*() 9&#xff0c…...

@Autowired 和 @Resource 的区别是什么?

Java面试题目录 Autowired 和 Resource 的区别是什么&#xff1f; Autowired 是 Spring 提供的注解。默认的注入方式为byType&#xff08;根据类型进行匹配&#xff09;。 Resource 是 JDK 提供的注解。默认注入方式为 byName&#xff08;根据名称进行匹配&#xff09;。 当一…...

栈和排序.

给你一个1->n的排列和一个栈&#xff0c;入栈顺序给定 你要在不打乱入栈顺序的情况下&#xff0c;对数组进行从大到小排序 当无法完全排序时&#xff0c;请输出字典序最大的出栈序列 输入 第一行一个数n 第二行n个数&#xff0c;表示入栈的顺序&#xff0c;用空格隔开&…...

springboot 多数据源怎么配置在控制台的sql打印日志

程序员的公众号&#xff1a;源1024&#xff0c;获取更多资料&#xff0c;无加密无套路&#xff01; 最近整理了一波电子书籍资料&#xff0c;包含《Effective Java中文版 第2版》《深入JAVA虚拟机》&#xff0c;《重构改善既有代码设计》&#xff0c;《MySQL高性能-第3版》&…...

【WinForms 窗体】常见的“陷阱”

当涉及到 WinForms 窗体编程时&#xff0c;我们可能会遇到一些常见的问题。在本篇博客中&#xff0c;我将为你提供一些常见问题的解决方案。 跨线程访问控件 在 WinForms 中&#xff0c;当在非UI线程上执行操作并尝试访问 UI 控件时&#xff0c;会引发跨线程访问异常。为了解决…...

Android readelf 工具查找函数符号

ELF&#xff08;Executable and Linkable Format&#xff09;是一种执行文件和可链接文件的格式。它是一种通用的二进制文件格式&#xff0c;用于在各种操作系统中存储可执行程序、共享库和内核模块。 Android 开发当中的 so 库本质上就是一种特殊类型的 ELF 文件&#xff0c;…...

MySQL-索引回顾

索引是面试高频问答题&#xff0c;参考百度/CSDN/尚硅谷/黑马程序员/阿里云开发者社区&#xff0c;决定将索引知识回顾一下&#xff0c;忘记时&#xff0c;点开即可&#xff0c;时刻保持更新&#xff0c;事不宜迟&#xff0c;即刻享用。 索引概述 索引&#xff08;index&#…...

重新认识Elasticsearch-一体化矢量搜索引擎

前言 2023 哪个网络词最热&#xff1f;我投“生成式人工智能”一票。过去一年大家都在拥抱大模型&#xff0c;所有的行业都在做自己的大模型。就像冬日里不来件美拉德色系的服饰就会跟不上时代一样。这不前段时间接入JES&#xff0c;用上好久为碰的RestHighLevelClient包。心血…...

【附源码】基于SSM框架的房屋租赁系统的设计与实现

基于SSM框架的房屋租赁系统的设计与实现 &#x1f345; 作者主页 央顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; 项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&…...

[SpringBoot]如何在一个普通类中获取一个Bean

最近在项目中出现了一个这种情况&#xff1a;我一顿操作猛如虎的写了好几个设计模式&#xff0c;然后在设计模式中的类中想将数据插入数据库&#xff0c;因此调用Mapper持久层&#xff0c;但是数据怎么都写不进去&#xff0c;在我一顿操作猛如虎的查找下&#xff0c;发现在普通…...

[ERROR] 不再支持目标选项 5。请使用 7 或更高版本

在编译spirng boot 3.x版本时,出现了以下错误. 出现这个错误: [ERROR] COMPILATION ERROR : [INFO] -------------------------------------------- [ERROR] 不再支持源选项 5。请使用 7 或更高版本。 [ERROR] 不再支持目标选项 5。请使用 7 或更高版本。 要指定版本: 解决办…...

EasyMR:为 AI 未来赋能,打造弹性大数据引擎的革命

如果要评一个2023科技圈的热搜榜&#xff0c;那么以人工智能聊天机器人 ChatGPT 为代表的 AI大模型 绝对会霸榜整个2023。 ChatGPT 于2022年11月30日发布。产品发布5日&#xff0c;注册用户数就超过100万。推出仅两个月后&#xff0c;它在2023年1月末的月活用户已经突破了1亿&…...

C //练习 4-10 另一种方法是通过getline函数读入整个输入行,这种情况下可以不使用getch与ungetch函数。请运用这一方法修改计算器程序。

C程序设计语言 &#xff08;第二版&#xff09; 练习 4-10 练习 4-10 另一种方法是通过getline函数读入整个输入行&#xff0c;这种情况下可以不使用getch与ungetch函数。请运用这一方法修改计算器程序。 注意&#xff1a;代码在win32控制台运行&#xff0c;在不同的IDE环境下…...

竞赛保研 基于深度学习的行人重识别(person reid)

文章目录 0 前言1 技术背景2 技术介绍3 重识别技术实现3.1 数据集3.2 Person REID3.2.1 算法原理3.2.2 算法流程图 4 实现效果5 部分代码6 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 基于深度学习的行人重识别 该项目较为新颖&#xff0c;适合…...

Ncast盈可视 高清智能录播系统 IPSetup.php信息泄露+RCE漏洞复现(CVE-2024-0305)

0x01 产品简介 Ncast盈可视 高清智能录播系统是广州盈可视电子科技有限公司一种先进的音视频录制和播放解决方案,旨在提供高质量、高清定制的录播体验。该系统采用先进的摄像和音频技术,结合强大的软件平台,可以实现高清视频录制、多路音频采集、实时切换和混音、定制视频分…...

GO语言Context的作用

文章目录 Context为什么需要Context多任务超时例子Context结构 Context各种使用方法创建contextvalueCtxvalueCtx结构体WithValue cancelCtxcancelCtx结构体withCancel timerCtxWithDeadlineWithTimeout 总结 Context 为什么需要Context Go语言需要Context主要是为了在并发环…...

金和OA C6 upload_json 任意文件上传漏洞

产品介绍 金和网络是专业信息化服务商,为城市监管部门提供了互联网监管解决方案,为企事业单位提供组织协同OA系统开发平台,电子政务一体化平台,智慧电商平台等服务。 漏洞概述 金和 OA C6 upload_json接口处存在任意文件上传漏洞&#xff0c;攻击者可以通过构造特殊请求包上…...

大模型学习第四课

学习目标&#xff1a; XTuner 大模型单卡低成本微调实战 学习内容&#xff1a; Finetune简介XTuner介绍8GB显卡玩转LLM动手实战环节 学习时间&#xff1a; 20240110 学习产出&#xff1a; Finetune简介 增量预训练微调指令跟随微调LoRA,QLoRAXTuner 简介&#xff1a;适配多…...

Code Runner使用外部控制台,运行结束后等待用户输入

问题描述 网上让程序运行结束暂停的方法大多数只有两种&#xff1a; 1.末尾加上system(“pause”) 2.start /k cmd 第一种方法每一个程序都需要在最后加上这条命令很烦&#xff1b; 第二章方法cmd窗口在程序运行结束后不会自动关闭&#xff0c;需要用户手动关闭 我想找到一种…...

IC设计的前端和后端是如何区分的?

一、工作着重点不同 **1、IC前端&#xff1a;**根据芯片规格书完成SOC的设计和集成&#xff0c; 使用仿真验证工具完成SOC的设计验证。 **2、IC后端&#xff1a;**将前端设计产生的门级网表通过EDA设计工具进行布局布线和进行物理验证并最终产生供制造用的GDSII数据 二、工作…...

大数据零基础学习day1之环境准备和大数据初步理解

学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 &#xff08;1&#xff09;设置网关 打开VMware虚拟机&#xff0c;点击编辑…...

macOS多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用

文章目录 问题现象问题原因解决办法 问题现象 macOS启动台&#xff08;Launchpad&#xff09;多出来了&#xff1a;Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用。 问题原因 很明显&#xff0c;都是Google家的办公全家桶。这些应用并不是通过独立安装的…...

vue3 定时器-定义全局方法 vue+ts

1.创建ts文件 路径&#xff1a;src/utils/timer.ts 完整代码&#xff1a; import { onUnmounted } from vuetype TimerCallback (...args: any[]) > voidexport function useGlobalTimer() {const timers: Map<number, NodeJS.Timeout> new Map()// 创建定时器con…...

OpenLayers 分屏对比(地图联动)

注&#xff1a;当前使用的是 ol 5.3.0 版本&#xff0c;天地图使用的key请到天地图官网申请&#xff0c;并替换为自己的key 地图分屏对比在WebGIS开发中是很常见的功能&#xff0c;和卷帘图层不一样的是&#xff0c;分屏对比是在各个地图中添加相同或者不同的图层进行对比查看。…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)

目录 一、&#x1f44b;&#x1f3fb;前言 二、&#x1f608;sinx波动的基本原理 三、&#x1f608;波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、&#x1f30a;波动优化…...

使用Matplotlib创建炫酷的3D散点图:数据可视化的新维度

文章目录 基础实现代码代码解析进阶技巧1. 自定义点的大小和颜色2. 添加图例和样式美化3. 真实数据应用示例实用技巧与注意事项完整示例(带样式)应用场景在数据科学和可视化领域,三维图形能为我们提供更丰富的数据洞察。本文将手把手教你如何使用Python的Matplotlib库创建引…...

代码随想录刷题day30

1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币&#xff0c;另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额&#xff0c;返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

Modbus RTU与Modbus TCP详解指南

目录 1. Modbus协议基础 1.1 什么是Modbus? 1.2 Modbus协议历史 1.3 Modbus协议族 1.4 Modbus通信模型 🎭 主从架构 🔄 请求响应模式 2. Modbus RTU详解 2.1 RTU是什么? 2.2 RTU物理层 🔌 连接方式 ⚡ 通信参数 2.3 RTU数据帧格式 📦 帧结构详解 🔍…...

Spring Boot + MyBatis 集成支付宝支付流程

Spring Boot MyBatis 集成支付宝支付流程 核心流程 商户系统生成订单调用支付宝创建预支付订单用户跳转支付宝完成支付支付宝异步通知支付结果商户处理支付结果更新订单状态支付宝同步跳转回商户页面 代码实现示例&#xff08;电脑网站支付&#xff09; 1. 添加依赖 <!…...

Vue 3 + WebSocket 实战:公司通知实时推送功能详解

&#x1f4e2; Vue 3 WebSocket 实战&#xff1a;公司通知实时推送功能详解 &#x1f4cc; 收藏 点赞 关注&#xff0c;项目中要用到推送功能时就不怕找不到了&#xff01; 实时通知是企业系统中常见的功能&#xff0c;比如&#xff1a;管理员发布通知后&#xff0c;所有用户…...