【C++】——list

news2024/9/19 8:07:29

文章目录

  • list介绍和使用
    • list注意事项
  • list模拟实现
  • list和vector的不同

list介绍和使用

在C++中,list是一个带头双向链表

在这里插入图片描述

list注意事项

  1. 迭代器失效
    删除元素:当使用迭代器删除一个元素时,指向该元素的迭代器会失效,但是不会影响其他迭代器。
  2. 内部实现:与vector相比,list不支持随机访问,就是说不能通过下标访问元素,但是可以使用迭代器 ,在删除和插入时非常方便
  3. 元素唯一性:list中的元素是不重复的,如果尝试插入已经存在的元素,该元素将被覆盖。
  4. 容量:list没有容量概念,根据动态分配内存
  5. 内存效率:他的插入和删除操作时间复杂度都为O(1)

list模拟实现

  1. 指针可以解引用,迭代器的类中必须重载operator*()
  2. 指针可以通过->访问其所指空间成员,迭代器类中必须重载oprator->()
  3. 指针可以++向后移动,迭代器类中必须重载operator++()与operator++(int)
    至于operator–()/operator–(int)释放需要重载,
    根据具体的结构来抉择,双向链表可以向前 移动,所以需要重载,如果是forward_list就不需要重载–
  4. 迭代器需要进行是否相等的比较,因此还需要重载operator==()与operator!=()
#pragma once
#include <assert.h>
namespace rq
{
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& x = T())
			:_data(x)
			, _next(nullptr)
			, _prev(nullptr)
		{}
	};
	template<class T>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T> Self;
		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{}
		T& operator*()
		{
			return _node->_data;
		}
		T* operator&()
		{
			return _node->_data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		bool operator!=(const Self& x)
		{
			return _node != x._node;
		}
		bool operator==(const Self& x)
		{
			return _node == x._node;
		}
	};
		
	template<class T>
	struct list_const_iterator
	{
		typedef list_node<T> Node;
		typedef list_const_iterator<T> Self;
		Node* _node;

		list_const_iterator(Node* node)
			:_node(node)
		{}
		
		const T& operator*()
		{
			return _node->_data;
		}
		const T* operator&()
		{
			return _node->_data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		bool operator!=(const Self& x)
		{
			return _node != x._node;
		}
		bool operator==(const Self& x)
		{
			return _node == x._node;
		}
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;

	public:
		typedef list_iterator<T> iterator;
		typedef list_const_iterator<T> const_iterator;


		void clear()
		{
			auto it = begin();
			while (it != end())
			{
				it = erase(it);//erase以后会返回下一个位置
			}
		}
		void emtpy_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			emtpy_init();
		}
		//拷贝构造
		list(const list<T>& lt)
		{
			emtpy_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

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

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

		/*	return iterator(_head->_next); 匿名对象*/
			return _head->_next;//接收指针,但是返回会隐式转换成iterator类型
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin() const 
		{
			/*iterator it(_head->_next);
			return it;*/

			/*	return iterator(_head->_next); 匿名对象*/
			return _head->_next;//接收指针,但是返回会隐式转换成iterator类型
		}
		const_iterator end() const 
		{
			return _head;
		}
		
		size_t size() const
		{
			return _size;
		}
		void swap(list<int>& it)
		{
			std::swap(_head, it._head); 
			std::swap(_size, it._size);
		}
		bool empty() const
		{
			return _size == 0;
		}
		void push_back(const T& x)
		{
			/*Node* newnode = new Node(x);
			Node* tail = _head->_prev;

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
			++_size;*/
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			erase(begin());
		}
		void pop_back()
		{
			erase(--end());
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());//不能把头节点删了
			Node* prev = pos._node->_prev;
			Node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			--_size;

			return next;
		}
		iterator insert(iterator pos, const T& x)
		{
			//prev newnode cur
			Node* newnode = new Node(x);
			Node* prev = pos._node->_prev;
			Node* cur = pos._node;

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			++_size;

			return newnode;
		}
	private:
		Node* _head;
		size_t _size;
	};
	template<class Container>
	void print_container(const Container& v)
	{
		list<int>::const_iterator it = v.begin();
		while (it != v.end())
		{
			//*it += 10;
			cout << *it << " ";
			++it;
		}
		cout << endl;
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_list01()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);
		
		print_container(lt);
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
}


test.cpp

#define   _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<string>
#include <algorithm>
#include <list>
using namespace std;
void test_list1()
{
	string s("sgfaiug");
	cout << s << endl;
	sort(s.begin(), s.end());
	cout << s << endl;
}
void test_list2()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	auto it = lt.begin();
	int k = 3;
	while (k--)
	{
		++it;
	}
	lt.insert(it, 30);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	int x = 0;
	cin >> x;
	it = find(lt.begin(), lt.end(), x);
	if (it != lt.end())//迭代器从前往后找,左闭右开,等于end就是没找到
	{
		lt.erase(it);
	}
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}
#include "list.h"
int main()
{
	//test_list2();
	rq::test_list01();
	return 0;
}

list和vector的不同

  1. 插入和删除:
    vector:是一个动态数组,它在内存中连续存储元素。这意味着vector可以支持快速的随机访问(通过下标),但插入和删除操作(尤其是在容器中间或开始位置)可能需要移动大量元素,因此可能相对较慢。
    list:是一个双向链表,每个元素都包含数据和指向列表中前一个和后一个元素的指针(或链接)。因此,list在插入和删除元素时(尤其是在容器中间或开始位置)非常高效,因为它只需要更改指针,而不需要移动其他元素。但是,list不支持快速的随机访问。
  2. 内存使用:vector使用的是一个连续的空间,空间利用率高,而list使用的是非连续的空间,空间利用率低
  3. 使用场景:
    如果你需要频繁地随机访问元素,且插入和删除操作较少,那么 vector 及其迭代器可能更适合。
    如果你需要频繁地在容器的中间进行插入和删除操作,那么 list 及其迭代器可能更合适。
  4. 性能:vector 的迭代器由于其随机访问特性,在需要快速访问容器中任意元素时非常高效。然而,在涉及大量插入或删除操作(尤其是在容器中间)时,可能会因为元素移动或内存重新分配而导致性能下降。
    list 的迭代器在遍历容器时通常较慢,因为它们不支持随机访问。但是,list 的插入和删除操作(特别是在容器的中间)通常比 vector 更快,因为它们不需要移动大量元素。

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

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

相关文章

订单防重复提交:token 发放以及校验

订单防重复提交&#xff1a;token 发放以及校验 1. 基于Token校验避免订单重复提交 1. 基于Token校验避免订单重复提交 在很多秒杀场景中&#xff0c;用户为了能下单成功&#xff0c;会频繁的点击下单按钮&#xff0c;这时候如果没有做好控制的话&#xff0c;就可能会给一个用…

ElementUI 布局——行与列的灵活运用

ElementUI 布局——行与列的灵活运用 一 . 使用 Layout 组件1.1 注册路由1.2 使用 Layout 组件 二 . 行属性2.1 栅格的间隔2.2 自定义元素标签 三 . 列属性3.1 列的偏移3.2 列的移动 在现代网页设计中&#xff0c;布局是构建用户界面的基石。Element UI 框架通过其强大的 <e…

面向对象程序设计之继承(C++)

1.继承的定义 1.1继承的概念 继承(inheritance)机制是⾯向对象程序设计使代码可以复⽤的最重要的⼿段&#xff0c;它允许我们在保持原有类特性的基础上进⾏扩展&#xff0c;增加⽅法(成员函数)和属性(成员变量)&#xff0c;这样产⽣新的类&#xff0c;称派⽣类。继承 呈现了⾯向…

Day26_0.1基础学习MATLAB学习小技巧总结(26)——数据插值

利用空闲时间把碎片化的MATLAB知识重新系统的学习一遍&#xff0c;为了在这个过程中加深印象&#xff0c;也为了能够有所足迹&#xff0c;我会把自己的学习总结发在专栏中&#xff0c;以便学习交流。 参考书目&#xff1a; 1、《MATLAB基础教程 (第三版) (薛山)》 2、《MATL…

Delphi CxGrid的主从表显示设置

界面编辑建立两个不同级别的视图层级-Layout 其实这是一个主从表关系&#xff0c; 1&#xff1a;填好主表的keyfieldnames 2&#xff1a;填好从表的keyfieldnames 3&#xff1a;填好从表的 detaikeyfieldNames与masterkeyfieldnames 4: 从表的数据源一定要按与主表关联的…

Vue实用操作-2-如何使用网页开发者工具

第一步&#xff0c;添加扩展&#xff0c;live服务器 第二步&#xff0c;将 favicon.ico 文件加入到根目录下 第三步&#xff0c;选择以服务器方式运行&#xff0c;并打开浏览器 第四步&#xff0c;在极简插件你中找到 vue 对应插件&#xff0c;安装到扩展插件中 第五步&#xf…

通过hosts.allow和hosts.deny限制用户登录

1、Hosts.allow和host.deny说明 两个文件是控制远程访问设置的&#xff0c;通过设置这个文件可以允许或者拒绝某个ip或者ip段的客户访问linux的某项服务。如果请求访问的主机名或IP不包含在/etc/hosts.allow中&#xff0c;那么tcpd进程就检查/etc/hosts.deny。看请求访问的主机…

【南方科技大学】CS315 Computer Security 【Lab2 Buffer Overflow】

目录 引言软件要求启动虚拟机环境设置禁用地址空间布局随机化&#xff08;ASLR&#xff09;设置编译器标志以禁用安全功能 概述BOF.ctestShellCode.c解释 createBadfile.c 开始利用漏洞在堆栈上查找返回地址 实验2的作业 之前有写过一个 博客&#xff0c;大家可以先看看栈溢出…

Qt ORM模块使用说明

附源码&#xff1a;QxOrm是一个C库资源-CSDN文库 使用说明 把QyOrm文件夹拷贝到自己的工程项目下, 在自己项目里的Pro文件里添加include($$PWD/QyOrm/QyOrm.pri)就能使用了 示例test_qyorm.h写了表的定义,Test_QyOrm_Main.cpp中写了所有支持的功能的例子: 通过自动表单添加…

C++——异常处理机制(try/catch/throw)

一、什么是异常处理机制 C++中的异常处理机制是一种用来检测和处理程序执行期间可能存在的异常情况的技术。它允许开发者编写健壮的代码,能够提前预判和处理程序执行可能会出现的错误,保证程序正常执行,而不会导致程序崩溃。 C++异常处理主要由几个关键字组成: try、cat…

C++笔记之std::map的实用操作

C++笔记之std::map的实用操作 code review 文章目录 C++笔记之std::map的实用操作1.初始化1.1.使用列表初始化1.2.使用 `insert` 方法1.3.使用 `emplace` 方法1.4.复制构造1.5.移动构造2.赋值2.1.列表赋值2.2.插入元素2.3.批量插入3.取值3.1.使用 `[]` 操作符3.2.使用 `at()` …

Vue路由配置、网络请求访问框架项目、element组件介绍学习

系列文章目录 第一章 基础知识、数据类型学习 第二章 万年历项目 第三章 代码逻辑训练习题 第四章 方法、数组学习 第五章 图书管理系统项目 第六章 面向对象编程&#xff1a;封装、继承、多态学习 第七章 封装继承多态习题 第八章 常用类、包装类、异常处理机制学习 第九章 集…

回归预测|基于开普勒优化相关向量机的数据回归预测Matlab程序KOA-RVM 多特征输入单输出 含基础RVM

回归预测|基于开普勒优化相关向量机的数据回归预测Matlab程序KOA-RVM 多特征输入单输出 含基础RVM 文章目录 一、基本原理1. **相关向量机&#xff08;RVM&#xff09;**2. **开普勒优化算法&#xff08;KOA&#xff09;**3. **KOA-RVM回归预测模型**总结 二、实验结果三、核心…

k8s集群备份与迁移

什么是 Velero? Velero 是一个用Go语言开发的开源工具&#xff0c;用于 Kubernetes 集群的备份、恢复、灾难恢复和迁移。 Velero备份工作流程 当用户发起velero backup create时&#xff0c;会执行如下四个动作&#xff1a; velero客户端调用Kubernetes API创建自定义资源并…

启动windows更新/停止windows更新,在配置更新中关闭自动更新的方法

在Windows操作系统中&#xff0c;启动或停止Windows更新&#xff0c;以及调整“配置更新”的关闭方法&#xff0c;涉及多种途径&#xff0c;这里将详细阐述几种常用的专业方法。 启动Windows更新 1.通过Windows服务管理器&#xff1a; -打开“运行”对话框&#xff08;…

15. 三数之和(实际是双指针类型的题目)

15. 三数之和 15. 三数之和 给你一个整数数组 nums &#xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k &#xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请你返回所有和为 0 且不重复的三元组。 注意&#xff1a;答案中不可以…

Uniapp的alertDialog返回值+async/await处理确定/取消问题

今天在使用uniui的alertDialog时&#xff0c;想添加一个确定/取消的警告框时 发现alertDialog和下面的处理同步进行了&#xff0c;没有等待alaertDialog处理完才进行 查询后发现问题在于 await 关键字虽然被用来等待 alertDialog.value.open() 的完成&#xff0c;但是 alertDi…

Android中的冷启动,热启动和温启动

在App启动方式中分为三种&#xff1a;冷启动&#xff08;cold start&#xff09;、热启动&#xff08;hot start&#xff09;、温启动&#xff08;warm start&#xff09; 冷启动&#xff1a; 系统不存在App进程&#xff08;App首次启动或者App被完全杀死&#xff09;时启动A…

使用 GaLore 预训练LLaMA-7B

项目代码&#xff1a; https://github.com/jiaweizzhao/galorehttps://github.com/jiaweizzhao/galore 参考博客&#xff1a; https://zhuanlan.zhihu.com/p/686686751 创建环境 基础环境配置如下&#xff1a; 操作系统: CentOS 7CPUs: 单个节点具有 1TB 内存的 Intel CP…

F12抓包11:UI自动化 - Recoder(记录器)

课程大纲 使用场景&#xff08;导入和导出&#xff09;: ① 测试的重复性工作&#xff0c;本浏览器录制并进行replay&#xff1b; ② 导入/导出录制脚本&#xff0c;移植后replay&#xff1b; ③ 导出给开发进行replay复现bug&#xff1b; ④ 进行前端性能分析。 1、录制脚…