当前位置: 首页 > 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数据 二、工作…...

百川2-13B-4bits开源大模型镜像免配置优势:内置check.sh脚本实现7维度健康检查

百川2-13B-4bits开源大模型镜像免配置优势&#xff1a;内置check.sh脚本实现7维度健康检查 1. 为什么说这个镜像"开箱即用"&#xff1f; 如果你之前部署过大语言模型&#xff0c;肯定经历过这些头疼事&#xff1a;环境配置报错、依赖包冲突、端口被占用、GPU显存不…...

多账号环境下的统一防火墙管理:AWS Firewall Manager + Network Firewall 分布式部署实战

placeholder...

从404到无损输出:一个Favicon抓取API的三年优化笔记(含CDN、懒加载避坑指南)

从404到毫秒响应&#xff1a;Favicon API架构演进与高并发实践 第一次收到用户反馈"favicon接口返回500错误"时&#xff0c;我们团队正在会议室讨论如何优化爬虫性能。那是个典型的周一早晨——咖啡还没喝完&#xff0c;警报先响了起来。这个看似简单的图标抓取服务&…...

Mac环境OpenClaw深度优化:Qwen3-4B模型推理速度提升30%方案

Mac环境OpenClaw深度优化&#xff1a;Qwen3-4B模型推理速度提升30%方案 1. 为什么需要优化OpenClaw的模型推理速度 上周我在用OpenClaw处理一个简单的文件整理任务时&#xff0c;发现整个流程耗时比预期长了近一倍。通过日志排查才发现&#xff0c;大部分时间都消耗在等待Qwe…...

用Multisim复刻经典:手把手教你搭建一个带分数显示的四人抢答器(附仿真文件)

用Multisim复刻经典&#xff1a;手把手教你搭建一个带分数显示的四人抢答器&#xff08;附仿真文件&#xff09; 在电子工程的学习和实践中&#xff0c;没有什么比亲手搭建一个完整的数字电路系统更能让人兴奋的了。尤其是对于那些对经典74系列芯片情有独钟的工程师和爱好者来说…...

化整为零、分而治之、异步编排:一文读懂现代并发的底层心法

LongAdder&#xff1a;化整为零&#xff0c;热点分散 在Java多线程编程中&#xff0c;‌原子变量&#xff08;如AtomicLong&#xff09;‌通过CAS操作实现线程安全的累加。然而&#xff0c;在高并发场景下&#xff0c;大量线程争抢同一原子变量会引发严重的‌缓存一致性问题‌。…...

Pixel Aurora Engine应用场景:独立开发者低成本构建像素IP资产库

Pixel Aurora Engine应用场景&#xff1a;独立开发者低成本构建像素IP资产库 1. 像素艺术创作新纪元 在游戏开发领域&#xff0c;像素艺术始终保持着独特的魅力。从早期的《超级马里奥》到现代的《星露谷物语》&#xff0c;像素风格游戏凭借其怀旧感和艺术表现力&#xff0c;…...

复旦微FMQL平台:memorytest工程实战指南与DDR稳定性验证

1. 从Procise导出memorytest工程 第一次接触复旦微FMQL平台时&#xff0c;我也被各种工程文件搞得晕头转向。memorytest工程作为内存测试的基础工具&#xff0c;其实导出过程比想象中简单得多。在Procise界面中找到memtest选项&#xff0c;就像在Windows资源管理器里找文件夹一…...

Kubernetes中的ConfigMap与Secret:安全高效管理配置的终极指南

引言&#xff1a;云原生时代的配置困境 在传统的运维模式中&#xff0c;配置往往硬编码在镜像中&#xff0c;或通过环境变量散落在各处。随着微服务架构的普及&#xff0c;这种模式带来了“配置漂移”、镜像臃肿、敏感信息泄露等痛点。 Kubernetes 通过 ConfigMap 和 Secret …...

内网渗透全流程拆解|从入门到实战,小白也能看懂的步骤

内网渗透不是“盲目尝试”&#xff0c;而是遵循固定流程的系统化操作&#xff0c;核心流程可概括为&#xff1a;信息收集→漏洞利用→权限提升→横向移动→权限维持→痕迹清理&#xff0c;每个环节环环相扣&#xff0c;缺一不可。本文将结合小白易理解的实战场景&#xff0c;详…...