C++入门篇9---list

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.mfbz.cn/a/78824.html

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

相关文章

【Apollo】Apollo版本变迁里程碑

特点与改进 概述里程碑6.0版本特点及改进7.0版本特点及改进8.0版本特点及改进代码差异 主页传送门&#xff1a;&#x1f4c0; 传送 概述 Apollo (阿波罗)是一个开放的、完整的、安全的平台&#xff0c;将帮助汽车行业及自动驾驶领域的合作伙伴结合车辆和硬件系统&#xff0c;快…

IntellIJ Idea 连接数据库-MySql

前言&#xff1a;可以用mariaDB工具&#xff0c;在本地创建服务器主机和数据库&#xff0c;而后用intellIJ Idea尝试连接 MariaDB创建数据库练习 1.IntellIJ Idea打开界面右侧Database工具&#xff0c;选择MySQL数据库。 2.填写数据库账号密码&#xff0c;地址端口号&#xff…

判断平面中两射线是否相交的高效方法

1. 简介 最近在工作中遇到判断平面内两射线是否相交的问题。 对于这个问题的解决,常规的方法是将两条射线拓展为直线,计算直线的交点,而后判断交点是否在射线上。 这种方法,在思路上较为直观,也易于理解。然后,该方法在计算量上相对较大。对于少量射线间的交点计算尚可…

WPF国际化的实现方法(WpfExtensions.Xaml)

https://blog.csdn.net/eyupaopao/article/details/120090431 resx资源文件实现 resx资源文件&#xff0c;实现的过程比第一种复杂&#xff0c;但resx文件本身编辑比较简单&#xff0c;维护起来比较方便。需要用到的框架&#xff1a;WpfExtensions.Xaml 为每种语言添加.resx资…

移动通信系统的LMS自适应波束成形技术matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 ..................................................................... idxx0; while idxx&…

数据分析 | 调用Optuna库实现基于TPE的贝叶斯优化 | 以随机森林回归为例

1. Optuna库的优势 对比bayes_opt和hyperoptOptuna不仅可以衔接到PyTorch等深度学习框架上&#xff0c;还可以与sklearn-optimize结合使用&#xff0c;这也是我最喜欢的地方&#xff0c;Optuna因此特性可以被使用于各种各样的优化场景。 2. 导入必要的库及加载数据 用的是sklea…

Ordinals 之后,以太坊铭文协议 Ethscriptions 如何再塑 NFT 资产形态

随着加密市场的发展&#xff0c;NFT 赛道逐渐形成了其独有的市场。但在加密熊市的持续影响下&#xff0c;今年 NFT 赛道的发展充满坎坷与挑战。据 NFTGO 数据显示&#xff0c;截至 8 月 7 日&#xff0c;与去年相比&#xff0c;NFT 市值总计约 56.4 亿美元&#xff0c;过去 1 年…

【业务功能篇69】Springboot 树形菜单栏功能设计

业务场景: 系统的界面&#xff0c;前端设计的时候&#xff0c;一般会给一个菜单栏&#xff0c;顶部横向以及左侧纵向的导航栏菜单&#xff0c;这里后端返回菜单栏的时候&#xff0c;就涉及层级父子项的问题&#xff0c;所以返回数据的时候&#xff0c;我们需要按照树化形式返回…

SpringBoot 操作Redis、创建Redis文件夹、遍历Redis文件夹

文章目录 前言依赖连接 RedisRedis 配置文件Redis 工具类操作 Redis创建 Redis 文件夹查询数据遍历 Redis 文件夹 前言 Redis 是一种高性能的键值存储数据库&#xff0c;支持网络、可基于内存亦可持久化的日志型&#xff0c;而 Spring Boot 是一个简化了开发过程的 Java 框架。…

CSRF

文章目录 CSRF(get)CSRF(post)CSRF Token CSRF(get) 根据提示的用户信息登录 点击修改个人信息 开启bp代理&#xff0c;点击submit 拦截到请求数据包 浏览器关闭代理 刷新页面 CSRF(post) 使用BP生成CSRF POC post请求伪造&#xff0c;可以通过钓鱼网站&#xff0c;诱导用户去…

【LeetCode】买卖股票的最佳时机最多两次购买机会

买卖股票的最佳时机 题目描述算法分析程序代码 链接: 买卖股票的最佳时机 题目描述 算法分析 程序代码 class Solution { public:int maxProfit(vector<int>& prices) {int n prices.size();vector<vector<int>> f(n,vector<int>(3,-0x3f3f3f))…

数据生成 | MATLAB实现WGAN生成对抗网络数据生成

数据生成 | MATLAB实现WGAN生成对抗网络数据生成 目录 数据生成 | MATLAB实现WGAN生成对抗网络数据生成生成效果基本描述程序设计参考资料 生成效果 基本描述 1.WGAN生成对抗网络&#xff0c;数据生成&#xff0c;样本生成程序&#xff0c;MATLAB程序&#xff1b; 2.适用于MATL…

基于 Debian 12 的MX Linux 23 正式发布!

导读MX Linux 是基于 Debian 稳定分支的面向桌面的 Linux 发行&#xff0c;它是 antiX 及早先的 MEPIS Linux 社区合作的产物。它采用 Xfce 作为默认桌面环境&#xff0c;是一份中量级操作系统&#xff0c;并被设计为优雅而高效的桌面与如下特性的结合&#xff1a;配置简单、高…

阿里云云解析DNS核心概念与应用

文章目录 1.DNS解析基本概念1.1.DNS基本介绍1.2.域名的分层结构1.3.DNS解析原理1.4.DNS递归查询和迭代查询的区别1.5.DNS常用的解析记录 2.使用DNS云解析将域名与SLB公网IP进行绑定2.1.进入云解析DNS控制台2.2.添加域名解析记录2.3.验证解析是否生效 1.DNS解析基本概念 DNS官方…

C++超基础语法

&#x1f493;博主个人主页:不是笨小孩&#x1f440; ⏩专栏分类:数据结构与算法&#x1f440; C&#x1f440; 刷题专栏&#x1f440; C语言&#x1f440; &#x1f69a;代码仓库:笨小孩的代码库&#x1f440; ⏩社区&#xff1a;不是笨小孩&#x1f440; &#x1f339;欢迎大…

SSH远程连接MacOS catalina并进行终端颜色配置

一、开关SSH服务 在虚拟机上安装了MacOS catalina&#xff0c;想要使用SSH远程进行连接&#xff0c;但是使用“系统偏好设置”/“共享”/“远程登录”开关进行打开&#xff0c;却一直是正在启动“远程登录”&#xff1a; 难道是catalina有BUG&#xff1f;不过还是有方法的&…

AcrelEMS-SW智慧水务能效管理平台解决方案-安科瑞黄安南

系统概述 安科瑞电气具备从终端感知、边缘计算到能效管理平台的产品生态体系&#xff0c;AcrelEMS-SW智慧水务能效管理平台通过在污水厂源、网、荷、储、充的各个关键节点安装保护、监测、分析、治理装置&#xff0c;用于监测污水厂能耗总量和能耗强度&#xff0c;重点监测主要…

Spring系列七:声明式事务

&#x1f418;声明式事务 和AOP有密切的联系, 是AOP的一个实际的应用. &#x1f432;事务分类简述 ●分类 1.编程式事务: 示意代码, 传统方式 Connection connection JdbcUtils.getConnection(); try { //1.先设置事务不要自动提交 connection.setAutoCommit(false…

【爬虫】爬取旅行评论和评分

以马蜂窝“普达措国家公园”为例&#xff0c;其评论高达3000多条&#xff0c;但这3000多条并非是完全向用户展示的&#xff0c;向用户展示的只有5页&#xff0c;数了一下每页15条评论&#xff0c;也就是75条评论&#xff0c;有点太少了吧&#xff01; 因此想了个办法尽可能多爬…

Swagger

目录 简介 使用方式&#xff1a; 常用注解 简介 使用Swagger你只需要按照他的规范去定义接口及接口相关信息再通过Swagger衍生出来的一系列项目和工具&#xff0c;就可以做到生成各种格式的接口文档&#xff0c;以及在线接口调试页面等等。 官网&#xff1a;https://swagger…
最新文章