【C++教程从0到1入门编程】第十三篇:STL中list类的模拟实现

一、list的模拟实现

 
#include<iostream>
#include<assert.h>
#pragma once
namespace jyr
{
	template<class T>
	struct _list_node
	{
		_list_node<T>* _next;
		_list_node<T>* _prev;
		T _data;

		_list_node(const T& val = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(val)
		{}
	};

	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* node)
			:_node(node)
		{}

		//*it,返回_data
		Ref operator*()
		{
			return _node->_data;
		}
		//it->date
		Ptr operator->()
		{
			return &_node->_data;
		}
		//it++,返回还是一个迭代器,前置加加
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//前置--
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		//后置加加
		Self operator++(int)
		{
			Self tmp(*this);  //创建临时变量返回,后置加加,加加之前不变
			//_list_iterator<T>tmp = *this;
			_node = _node->_next;
			//++(*this)
			return tmp;
		}
		//后置减减
		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		//==
		bool operator ==(const Self& it)
		{
			return _node = it._node;
		}
		//it!=end()
		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}
	};

	template<class T>
	class list
	{
	public:
		typedef _list_node<T> Node;
	public:
		typedef _list_iterator<T,T&,T*> iterator;
		typedef _list_iterator<T,const T&,const T*> const_iterator;
		iterator begin()            //第一个数据的迭代器,节点的指针构建迭代器
		{
			return iterator(_head->_next);
		}
		iterator end()     
		{
			return iterator(_head);
		}

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


		//带头双向循环链表
		list()                      //list的构造
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		} 

		//l2(l1)
		list(const list<T>& l1)            //拷贝构造
		{
			//链表的最初状态
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			//const_iterator it = l1.begin();   //构造一个const迭代器尾插到新的迭代器中
			//while (it != l1.end())
			//{
			//	this->push_back(*it);
			//	it++;
			//}

			for (auto e : l1)
			{
				push_back(e);
			}
		}

		list<T>& operator=(const list<T>& l1)
		{
			if (this != &l1)
			{
				clear();
				for (auto e : l1)
				{
					push_back(e);
				}
			}
			return *this;
		}
		
		//现代版的拷贝构造
		//list<T>& operator=(list<T> l1)
		//{
		//	swap(_head, l1._head);
		//	return *this;
		//}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);         //erase要自己单独实现出来
			}
		}
		~list()                      //析构函数
		{
			clear();                 //clear没有删除头结点
			delete _head;
			_head = nullptr;
		}

		size_t size()  const
		{

		}
		bool empty() const
		{
			return _head->_next == _head->_prev;
		}
		//T x  如果T类型是vector或者是string就造成了深拷贝,那么就得传引用,传引用不改变x就加const
		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;
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void insert(iterator pos, const T& x)    //在pos这个迭代器的前面插入一个节点
		{
			Node* newnode = new Node(x);
			//pos 是个迭代器,而迭代器又是节点的指针,所以可以通过迭代器找到这个节点
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			//return iterator(newnode);
		}

		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());//end是最后一个有效数据的下一个位置,所以end先减减,删除end减减的位置
		}
		void erase(iterator pos)
		{
			//pos是一个迭代器,节点的指针,可以找到这个节点
			//找到要删除的节点del
			assert(pos != end());//头结点不能删除
			Node* del = pos._node;
			Node* next = del->_next;
			//假设有1 2 3节点,要删除pos位置2节点,
			//先2节点的前一个节点1节点的下一个节点指向2指向的3节点
			//然后要把3和1链接起来,把3的前一个节点指向1,
			//2的下一个节点3节点的前一个节点指向2的前一个节点
			//删除2
			del->_prev->_next = del->_next; 
			del->_next->_prev = del->_prev;
			delete del;
			//return iterator(next);//返回节点的下一个位置
		}
	private:
		Node* _head;
	};
	void testlist1()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(10);
		lt1.push_back(20);
		lt1.pop_back();
		list<int>::iterator it = lt1.begin();
		while (it != lt1.end())
		{
			std::cout << *it << " ";
			++it;
		}
		std::cout << std::endl;
	}
	struct Date
	{
		int _year = 0;
		int _month = 1;
		int _day = 1;
	};

	void print_list(const list<int>& lt1)
	{
		list<int>::const_iterator it = lt1.begin();
		while (it != lt1.end())
		{
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}
	void testlist2()
	{
		list<Date> lt1;
		lt1.push_back(Date());
		lt1.push_back(Date());
		list<Date>::iterator it = lt1.begin();
		while (it != lt1.end())
		{
			/*std::cout << *it << " ";*/
			//it->Date->_year  
			std::cout << it->_year << ":" << it->_month << ":" << it->_day << std::endl;
			std::cout << (*it)._year << ":" << (*it)._month << ":" << (*it)._day << std::endl;
			++it;
		}
		//print_list(lt1);
	}
	void testlist3()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		
		list<int>l2(l1); //拷贝构造,用l1拷贝构造l2
		for (auto e : l1)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
		for (auto e : l2)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
	}
}

二、list与vector之间的对比

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

主流公链 - Filecoin

探索Filecoin&#xff1a;去中心化存储网络 1. Filecoin简介 Filecoin是一个去中心化的存储网络&#xff0c;旨在通过区块链技术实现全球性的分布式文件存储和检索市场。Filecoin允许用户将文件存储在网络中的节点上&#xff0c;并通过加密、分片和复制等技术保证数据的安全性…

数据结构

一、栈 先进后出 二、队列 先进先出 三、数组 查询快&#xff0c;增加修改慢 四、链表 查询慢&#xff0c;增加修改慢 五、二叉树 节点&#xff1a; 查找二叉树 二叉查找树的特点 二叉查找树,又称二叉排序树或者二叉搜索树 每一个节点上最多有两个子节点 左子树上所…

QQ屠龙者终成为恶龙!工作与生活的平衡:挑战与解决方案——早读(逆天打工人爬取热门微信文章解读)

屠龙者终成为恶龙 引言Python 代码第一篇 洞见 看了上海临时工睡桥洞的现场&#xff0c;我不敢让老板知道上班苦第二篇 人民日报 来了&#xff01;新闻早班车要闻社会政策 结尾 昔日斩棘披荆的屠龙勇士 历经岁月涤荡 竟在不知不觉间铸就了自己心中的铁壁铜墙 终有一日 赫然发现…

【智能计算模拟:DFT+MD+ML深度融合及科研实践应用】

第一性原理、分子动力学与机器学习三者的交汇融合已在相关研究领域展现强劲的研究热潮。借助第一性原理计算揭示材料内在的量子特性&#xff0c;并结合分子动力学模拟探究材料在实际环境下的动态行为&#xff1b;运用机器学习算法与上述方法结合&#xff0c;开发高性能预测模型…

智能医疗-方案优势

实时更新&#xff0c;信息展示更便利 电子标签床头信息卡可设计特定的信息模板展示病患信息&#xff0c;并可根据治疗进展实时更新&#xff0c;病患通过床头电子标签即可了解病情信息。 —签多能&#xff0c;医护管理更高效 电子墨水屏技术改进了传统院内展示内容单一、更换成…

edga 0x800704cf错误码(同步失败)

edga 0x800704cf错误码(同步失败) 执行此操作需要 Internet。 你似乎没有连接到 Internet。请检查你的连接&#xff0c;然后再试一次。 0x800704cf 发送反馈 取消windows键R&#xff0c; 输入services.msc 进入本地服务管理&#xff0c; 重启 Microsoft Account Sign-in Assi…

【探索Linux】—— 强大的命令行工具 P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )

阅读导航 引言一、序列化反序列化概念二、各种语言序列化反序列化常用库三、C的JSON序列化库1. RapidJSON库&#xff08;1&#xff09;头文件&#xff08;2&#xff09;相关函数&#xff08;3&#xff09;参数&#xff08;4&#xff09;特点 &#xff08;5&#xff09;使用示例…

零拷贝技术、常见实现方案、Kafka中的零拷贝技术的使用、Kafka为什么这么快

目录 1. 普通拷贝 2. 数据拷贝基础过程 2.1 仅CPU方式 2.2 CPU&DMA方式 3.普通模式数据交互 4. 零拷贝技术 4.1 出现原因 4.2 解决思路 4.2.1 mmap方式 4.2.2 sendfile方式 4.2.3 sendfileDMA收集 4.2.4 splice方式 5. Kafka中使用到的零拷贝技术 参考链接 本…

windows下powershell与linux下bash美化教程(使用starship)

starship美化教程 Win11 Powershell 安装 在命令行使用下面命令安装 # 安装starship winget install starship将以下内容添加到 Microsoft.PowerShell_profile.ps1&#xff0c;可以在 PowerShell 通过 $PROFILE 变量来查询文件的位置 Invoke-Expression (&starship i…

gitee多用户配置

一、引言 在工作的时候我们有时候会自己创建项目Demo来实现一些功能&#xff0c;但是又不想把自己的Demo代码放到公司的仓库代码平台&#xff08;gitee&#xff09;中管理&#xff0c;于是就是想自己放到自己的Gitee中管理&#xff0c;于是就需要配置Git多用户。 本文将配置分别…

【4月】组队打卡《山有木兮Python数据分析极简入门》

活动名称 CDA Club 第2期《山有木兮Python数据分析极简入门》组队打卡 活动介绍 本次打卡活动由CDA俱乐部旗下学术部主办。目的是通过数据分析科普内容&#xff0c;为数据分析爱好者提供学习和交流的机会。方便大家利用碎片化时间在线学习&#xff0c;以组队打卡的形式提升学…

Kindling the Darkness:A Practical Low-light Image Enhancer

Abstract 在弱光条件下拍摄的图像通常会出现&#xff08;部分&#xff09;可见度较差的情况。,除了令人不满意的照明之外&#xff0c;多种类型的退化也隐藏在黑暗中&#xff0c;例如由于相机质量有限而导致的噪点和颜色失真。,换句话说&#xff0c;仅仅调高黑暗区域的亮度将不…

Altair(澳汰尔) Radioss® 评估和优化动态载荷下的高度非线性问题

Altair&#xff08;澳汰尔&#xff09; Radioss 评估和优化动态载荷下的高度非线性问题 Radioss 是一款超前的分析解决方案&#xff0c;可评估和优化动态载荷下的高度非线性问题。它广泛应用于全球各行各业&#xff0c;能有效提高复杂设计的耐撞性、安全性和可制造性。 30 多…

iOS - Runtime - Class的结构

文章目录 iOS - Runtime - Class的结构前言1. Class的结构1.1 Class的结构1.1.1 objc_class1.1.2 class_rw_t1.1.3 class_ro_t 1.2 class_rw_t和class_ro_t的区别1.3 class_rw_t和class_ro_t的关系1.3.1 分析关系1.3.2 原因 1.4 method_t1.4.1 Type Encoding1.4.2 types iOS - …

00000基础搭建vue+flask前后端分离项目

我完全是参考的这个vue3flask前后端分离环境速建_flask vue3-CSDN博客 安装了node_js&#xff08;添加了环境变量&#xff09; 环境变量 把原来的镜像源换成了淘宝镜像源 npm config set registry https://registry.npmmirror.com/ 查看版本证明安装成功 npm - v 安装npm i…

web自动化测试系列-selenium的运行原理和常用方法介绍(二)

目录 1.selenium的运行原理 2.常用方法介绍 接上文 &#xff1a;web自动化测试系列-selenium的安装和运行(一)-CSDN博客 在上文中我们编写了一段简单的代码 &#xff0c;可以驱动浏览器访问百度并搜索关键字 。这里我们再把这段代码再拿来加以说明 。 # 1. 导包 from selen…

gitee拉取与推送

&#x1f331;博客主页&#xff1a;青竹雾色间 &#x1f618;博客制作不易欢迎各位&#x1f44d;点赞⭐收藏➕关注 目录 一&#xff0c;从本地推送项目到gitee1.首先我们在gitee上创建一个仓库2.clone远程仓库到本地3.git的三板斧3.1. add - 将代码添加到本地仓库3.2. commit …

入行AI写作第一个月收入2万+复盘分享

AI写作作为一种新兴的创作方式&#xff0c;正逐渐改变着内容产业的生态。在这个领域中&#xff0c;许多人通过自己的努力和智慧&#xff0c;实现了快速的成长和收入的增长。本文将从技术学习与掌握、实践与应用、内容创作与优化、持续学习与创新、总结与复盘这五个方面&#xf…

Python时间序列异常检测库之adtk使用详解

概要 ADTK(Anomaly Detection Toolkit)是一个用于时间序列异常检测的Python库。它提供了一系列工具来识别时间序列数据中的异常模式,适用于多种监测和预测任务。 ADTK简介 ADTK专注于时间序列异常检测,支持多种异常检测算法,包括统计学方法、机器学习模型以及基于规则的…

配置Web运行环境与第一个网页

安装与配置Web环境: 如下使用了VSC作为web的运行环境。 下面是VSC的官网点击进入:Download Visual Studio Code - Mac, Linux, Windowshttps://code.visualstudio.com/download 1.下载 进入官网后可以看到有windows&#xff0c;linux&#xff0c;还有苹果按照自己的系统下载&…