C++入门篇9---list

news2024/10/7 18:20:20

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)[fiirst,last)区间的元素构造list
void test1()
{
	list<int> v;
	list<int> v1(5,2);
	list<int> v2(v1);
	list<int> v3(v1.begin(),v1.end());
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	for (auto x : v1)
		cout << x << " ";
	cout << endl;

	for (auto x : v2)
		cout << x << " ";
	cout << endl;

	for (auto x : v3)
		cout << x << " ";
	cout << endl;

}

 2.list的迭代器

函数名称功能名称
begin()+end()获取第一个数据位置的iterator/const_iterator,获取最后一个数据的下一个位置的iterator/const_iterator
rbegin()+rend()获取第一个数据位置的reverse_iterator/const_reverse_iterator,获取最后一个数据的下一位置的reverse_iterator/const_reverse_iterator

 

void test2()
{
	list<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	list<int>::iterator it = v.begin();
	//注意如果写类型名,那么一定要写正确,如加不加reverse、const一定要写对
	//如果不想写这么长的类型,可以写auto自动类型推导
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	list<int>::reverse_iterator it1 = v.rbegin();
	while (it1 != v.rend())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
}

3.list的capacity

函数声明功能介绍
empty()检测list是否为空
size()返回list中有效结点的个数

 4.获取首尾元素

函数声明功能介绍
front返回list的第一个节点中值的引用
back返回list的最后一个结点中值的引用

5.list的修改

函数名称功能介绍
push_front在list首元素前插入值为val的值
pop_front删除list中第一个元素
push_back在list尾部插入值为val的值
pop_back删除list中的最后一个元素
insert在list中pos位置插入值为val的元素
erase删除list中pos位置的元素
swap交换两个list中的元素
clear清空list中的有效元素
void test3()
{
	list<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_front(1);
	v.push_front(2);
	v.push_front(3);
	v.push_front(4);
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	v.pop_back();
	v.pop_front();
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	v.insert(v.begin(),10);
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	v.erase(v.begin());
	for (auto x : v)
		cout << x << " ";
	cout << endl;
}

 6.list迭代器失效问题(重点

在讲vector的博客中,我也提到了迭代器失效问题,那么问个问题,list的迭代器失效和vector的迭代器失效一样吗?为什么?

这里先解释一下什么是迭代器,估计有很多人对这个名词还不是很了解,其实所谓的迭代器从作用上来说就是访问遍历容器的工具,它将所有容器的访问遍历方式进行了统一(vector,list,set等等容器的迭代器使用几乎一摸一样都是begin(),end(),++/--等操作),封闭了底层的细节,简化了我们对容器的使用,对于初学者来说,这玩意tm的太神了,但是如果我们了解它的底层实现,我们就会发现,迭代器不过是一层封装,底层还是数据结构那一套,如list链表,迭代器的++,本质还是指针的变化。

(容器的底层实现还是要了解一些,能够帮助我们更好的认识和使用容器,可以看看我写过的一些模拟实现,如果有需要注释或者详解,请在评论区留言,如果需求多,我会单独出一篇博客讲解一下里面的一些重点内容)

好,下面回归正题,如果你数据结构学的还不错并且知道vector的迭代器失效是扩容引起的,那么这个问题不难回答,因为链表的增查改不会影响一个结点的位置,除了删除操作,所以list的迭代器失效仅仅只有在删除list结点时才会出现,并且只有那个被删除结点的迭代器会失效,其他的不受影响

二、模拟实现list的基本功能

namespace zjs
{
	template <class T>
	struct list_node {
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

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

    //重点
	template <class T, class Ref, class Ptr >
	struct __list_iterator {
		typedef list_node<T> Node;
		typedef __list_iterator self;
		Node* node;

		__list_iterator(Node* x)
			:node(x)
		{}

		self& operator++()
		{
			node = node->_next;
			return *this;
		}

		self& operator--()
		{
			node = node->_prev;
			return *this;
		}

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

		self operator--(int)
		{
			self tmp(*this);
			node = node->_prev;
			return tmp;
		}

		Ref operator*()
		{
			return node->_data;
		}

		bool operator==(const self& It) const
		{
			return node == It.node;
		}

		bool operator!=(const self& It) const
		{
			return node != It.node;
		}

		Ptr operator->()
		{
			return &node->_data;
		}
	};

	/*template <class T>
	struct __list_const_iterator {
		typedef list_node<T> Node;
		typedef __list_const_iterator self;
		Node* node;

		__list_const_iterator(Node* x)
			:node(x)
		{}

		self& operator++()
		{
			node = node->_next;
			return *this;
		}

		self& operator--()
		{
			node = node->_prev;
			return *this;
		}

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

		self operator--(int)
		{
			self tmp(*this);
			node = node->_prev;
			return tmp;
		}

		const T& operator*() 
		{
			return node->_data;
		}

		const T* operator->()
		{
			return &node->_data;
		}

		bool operator==(const self& It)
		{
			return node == It.node;
		}

		bool operator!=(const self& It)
		{
			return node != It.node;
		}
	};*/

	template <class T>
	class list
	{
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T,const T&,const T*> const_iterator;
		//typedef __list_const_iterator<T> const_iterator;
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			_size = 0;
			empty_init();
		}
		
		void clear()
		{
			iterator it = begin();
			while (it!=end())
			{
				it = erase(it);
			}
		}
		
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		list(const list<T>& tmp)
			:_head(nullptr)
			,_size(0)
		{
			empty_init();
			for (auto& x : tmp)
			{
				push_back(x);
			}
		}

		void swap(list& tmp)
		{
			std::swap(_head, tmp._head);
			std::swap(_size, tmp._size);
		}

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

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

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

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

		iterator end()
		{
			//return iterator(_head);
			return _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;
			insert(end(), x);
		}

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

		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos.node;
			Node* pre = cur->_prev;
			Node* newnode = new Node(x);
			pre->_next = newnode;
			newnode->_prev = pre;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
			return newnode;
		}


		void pop_back()
		{
			erase(--end());
		}

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

		iterator erase(iterator pos)
		{
			Node* cur = pos.node;
			Node* pre = cur->_prev;
			Node* next = cur->_next;
			pre->_next = next;
			next->_prev = pre;
			delete cur;
			_size--;
			return next;
		}

		size_t size() const
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

    //模板的一些应用,typename的用法
    //这里只能用typedef,用来告诉编辑器const_iterator是一个类型名,而不是一个静态变量
    //因为编辑器在编译阶段要判断有没有语法错误,而list<T>没有实例化,就无法在里面
    //查找const_iterator,而如果它是静态变量很显然这是个语法错误,
    //所以这里要加上typename告诉编辑器这是个类型名,等到实例化之后再去里面找
	template<typename T>
	void print_list(const list<T>& s)
	{
		typename list<T>::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	template<typename container>
	void print_container(const container& s)
	{
		typename container::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

}

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

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

相关文章

推荐系统系列之推荐系统概览(上)

在当今信息化高速发展的时代&#xff0c;推荐系统是一个热门的话题和技术领域&#xff0c;一些云厂商也提供了推荐系统的SaaS服务比如亚马逊云科技的 Amazon Personalize 来解决客户从无到有迅速构建推荐系统的痛点和难点。在我们的日常生活中&#xff0c;推荐系统随处可见&…

论文阅读 - Understanding Diffusion Models: A Unified Perspective

文章目录 1 概述2 背景知识2.1 直观的例子2.2 Evidence Lower Bound(ELBO)2.3 Variational Autoencoders(VAE)2.4 Hierachical Variational Autoencoders(HVAE) 3 Variational Diffusion Models(VDM)4 三个等价的解释4.1 预测图片4.2 预测噪声4.3 预测分数 5 Guidance5.1 Class…

【项目设计】从零实现一个高并发内存池

​&#x1f320; 作者&#xff1a;阿亮joy. &#x1f386;专栏&#xff1a;《项目设计》 &#x1f387; 座右铭&#xff1a;每个优秀的人都有一段沉默的时光&#xff0c;那段时光是付出了很多努力却得不到结果的日子&#xff0c;我们把它叫做扎根 目录 &#x1f449;项目介绍&…

005-Spring 扩展点 :PostProcess

目录 Spring 扩展点 &#xff1a;PostProcess介绍PostProcess大纲文字明细使用方法示例Autowired 功能实现Resource 功能实现 后记 Spring 扩展点 &#xff1a;PostProcess 介绍 Spring 核心做的事情其实很简单就是&#xff1a;控制反转和依赖注入 也就是把 Class 解析为 Bea…

npm install ffi各种失败,换命令npm i ffi-napi成功

网上各种帖子安装ffi&#xff0c;基本上到了windows build tools这里会卡住。 使用命令npm install --global --production windows-build-tools 安装报错信息如下&#xff1a; PS E:\codes\nodejsPath\tcpTest> npm install --global --production windows-build-tools …

06-加密算法

加密算法 一、前言知识1、加密解密2、MD5&#xff08;最常见&#xff09;3、SHA4、进制5、时间戳6、URL编码7、base64编码8、unescape编码9、AES加密10、DES&#xff08;类似于base64&#xff09; 二、常见加密形式算法解析三、演示案例1、某 CTF 比赛题目解析2、某 CMS 密码加…

【不带权重的TOPSIS模型详解】——数学建模

目录索引 定义&#xff1a;问题引入&#xff1a;不合理之处&#xff1a;进行修改&#xff1a; 指标分类&#xff1a;指标正向化&#xff1a;极小型指标正向化公式&#xff1a;中间型指标正向化公式&#xff1a;区间型指标正向化公式&#xff1a; 标准化处理(消去单位)&#xff…

pytorch报错torch.cuda.is_available()结果false处理方法

文章目录 问题及起因问题起因 解决方法 问题及起因 问题 前几天跑项目&#xff0c;笔记本上的GPU可以正常跑起来。要跑VAE模型&#xff0c;重新安装了torch,GPU就无法使用了&#xff0c;我重新安装了 cuda,torch.cuda.is_available()的结果依然是False。 起因 配置项目环境…

测试人员的BUG防不胜防

“灵异事件&#xff01;程序里发现了新Bug但是它正常运行啦&#xff01;”、“谁敢信&#xff0c;我电脑死机竟然是因为放青藏高原的时候硬盘共振振幅太大了——”…… 人生处处有Bug&#xff0c;哪一个最令你目瞪口呆&#xff0c;久久不能忘怀&#xff1f;今天就来浅浅分享一…

Spring系列篇--关于IOC【控制反转】的详解

&#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 接下来看看由辉辉所写的关于Spring的相关操作吧 目录 &#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 一.什么是Spring 二.Spring的特点 三.什…

TCP服务器—实现数据通信

目录 前言 1.接口介绍 2.编写服务器 3.编写客户端 4.编译链接 5.测试 6.总结 前言 今天我们要介绍的是使用TCP协议实现数据通信&#xff0c;相比于之前写的UDP服务器实现数据信&#xff0c;在主体逻辑上并没有差别。客户端向服务器发送信息&#xff0c;服务器接受信息并回…

如何进行无线网络渗透测试?

今天我们将继续深入探讨Kali Linux的应用&#xff0c;这次我们将重点介绍如何使用Kali Linux进行无线网络渗透测试。无线网络渗透测试是评估无线网络安全性的重要步骤&#xff0c;而Kali Linux作为一款专业的渗透测试发行版&#xff0c;提供了丰富的工具来帮助你进行这项任务。…

支持https访问

文章目录 1. 打开自己的云服务器的 80 和 443 端口2. 安装 nginx3. 安装 snapd4. 安装 certbot5. 生成证书6. 拷贝生成的证书到项目工作目录7. 修改 main.go 程序如下8. 编译程序9. 启动程序10. 使用 https 和端口 8081 访问页面成功11. 下面修改程序&#xff0c;支持 https 和…

【RocketMQ】NameServer总结

NameServer是一个注册中心&#xff0c;提供服务注册和服务发现的功能。NameServer可以集群部署&#xff0c;集群中每个节点都是对等的关系&#xff08;没有像ZooKeeper那样在集群中选举出一个Master节点&#xff09;&#xff0c;节点之间互不通信。 服务注册 Broker启动的时候会…

grafana-zabbix基础操作篇------导入数据源

文章目录 一、grafana的安装1.1、下载地址1.2、下载后导入所安装机器1.3、yum安装解决依赖1.4、启动grafana1.5、查看端口是否启用&#xff08;端口默认3000&#xff09;1.6、浏览器访问 二、添加zabbix数据源2.1、导入数据源 **下一篇 我们讲讲构建仪表板的操作** 今天&#x…

SpringMVC拦截器的介绍,拦截器的基本实现,拦截器链配置

&#x1f40c;个人主页&#xff1a; &#x1f40c; 叶落闲庭 &#x1f4a8;我的专栏&#xff1a;&#x1f4a8; c语言 数据结构 javaweb 石可破也&#xff0c;而不可夺坚&#xff1b;丹可磨也&#xff0c;而不可夺赤。 拦截器 一、拦截器概念二、拦截器与过滤器的区别三、拦截器…

【ChatGLM】ChatGLM-6B模型Win+4GB显卡本地部署笔记

ChatGLM-6B是清华大学知识工程和数据挖掘小组发布的一个类似ChatGPT的开源对话机器人&#xff0c;由于该模型是经过约1T标识符的中英文训练&#xff0c;且大部分都是中文&#xff0c;因此十分适合国内使用。 预期环境 本机电脑备注&#xff1a; Win10专业版 32G内存256固态系统…

【BASH】回顾与知识点梳理(三十一)

【BASH】回顾与知识点梳理 三十一 三十一. 进程的管理31.1 给进程发送讯号kill -signal PIDlinux系统后台常驻进程killall -signal 指令名称 31.2 关于进程的执行顺序Priority 与 Nice 值nice &#xff1a;新执行的指令即给予新的 nice 值renice &#xff1a;已存在进程的 nice…

绿盾客户端文件加密不显示锁的图标,加密功能正常

环境: 绿盾客户端7.0 Win10 专业版 问题描述: 绿盾客户端文件加密不显示锁的图标,加密功能正常 解决方案: 1.查看控制台是否设置隐藏图标 (未解决) 控制台-规则中心-安全选项-“加密文件显示加密图标”和“不显示Explorer 鼠标右键菜单”是否打钩 如果没打钩,则不…

(学习笔记-进程管理)怎么避免死锁?

死锁的概念 在多线程编程中&#xff0c;我们为了防止多线程竞争共享资源而导致数据错乱&#xff0c;都会在操作共享资源之前加上互斥锁&#xff0c;只有成功获得到锁的线程&#xff0c;才能操作共享资源&#xff0c;获取不到锁的线程就只能等待&#xff0c;直到锁被释放。 那…