C++:list容器--模拟实现(下篇)

1. 模拟实现 list 一些常用接口

// list.h
#pragma once
#include <assert.h>
#include "Iterator.h"namespace room
{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> Self;Node* _node;list_iterator(Node* node):_node(node){}// 解引用运算符重载Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}// 前置++运算符重载Self& operator++(){_node = _node->_next;return *this;}// 后置++运算符重载Self operator++(int){Self tmp(*this);_node = _node->_next;return tmp;}// 前置--运算符重载Self& operator--(){_node = _node->_prev;return *this;}// 后置--运算符重载Self operator--(int){Self tmp(*this);_node = _node->_prev;return tmp;}// !=运算符重载bool operator!=(const Self& it){return _node != it._node;}bool operator==(const Self& it){return _node == it._node;}};//// 迭代器//template<class T>//struct list_iterator//{//	typedef list_node<T> Node;//	Node* _node;//	list_iterator(Node* node)//		:_node(node)//	{}//	// 解引用运算符重载//	T& operator*()//	{//		return _node->_data;//	}//	T* operator->()//	{//		return &_node->_data;//	}//	//	// ++运算符重载//	list_iterator<T>& operator++()//	{//		_node = _node->_next;//		return *this;//	}//	// !=运算符重载//	bool operator!=(const list_iterator<T>& it)//	{//		return _node != it._node;//	}//	bool operator==(const list_iterator<T>& it)//	{//		return _node == it._node;//	}//};//// 迭代器//template<class T>//struct list_const_iterator//{//	typedef list_node<T> Node;//	Node* _node;//	list_const_iterator(Node* node)//		:_node(node)//	{}//	// 解引用运算符重载//	const T& operator*()//	{//		return _node->_data;//	}//	const T* operator->()//	{//		return &_node->_data;//	}//	// ++运算符重载//	list_const_iterator<T>& operator++()//	{//		_node = _node->_next;//		return *this;//	}//	// !=运算符重载//	bool operator!=(const list_const_iterator<T>& it)//	{//		return _node != it._node;//	}//	bool operator==(const list_const_iterator<T>& it)//	{//		return _node == it._node;//	}//};template<class T>class list{typedef list_node<T> Node;public:/*typedef Node* iterator;typedef list_iterator<T> iterator;typedef list_const_iterator<T> const_iterator;*/typedef list_iterator<T, T&, T*> iterator;typedef list_iterator<T, const T&, const T*> const_iterator;typedef ReverseIterator<iterator, T&, T*> reverse_iterator;typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;// 第一种写法//reverse_iterator rbegin()//{//	return reverse_iterator(--end());//}//reverse_iterator rend()//{//	return reverse_iterator(end());//}// 对称reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}const_reverse_iterator rbegin() const{return const_reverse_iterator(end());}const_reverse_iterator rend() const{return const_reverse_iterator(begin());}iterator begin(){//return iterator(_head->_next);iterator it(_head->_next);return it;}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}void empty_init(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(){/*_head = new Node;_head->_prev = _head;_head->_next = _head;*/empty_init();}list(initializer_list<T> lt){empty_init();for (auto& e : lt){push_back(e);}}// lt2(lt1)list(const list<T>& lt){empty_init();for (auto& e : lt){push_back(e);}}// lt1 = lt3list<T>& operator=(list<T> lt){swap(lt);return *this;}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}~list(){clear();delete _head;_head = nullptr;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}size_t size() const{return _size;}void push_back(const T& x){/*Node* newnode = new Node(x);Node* tail = _head->_prev;newnode->_prev = tail;tail->_next = newnode;newnode->_next = _head;_head->_prev = newnode;*/insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_front(){erase(begin());}void pop_back(){erase(--end());}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;++_size;}iterator erase(iterator pos){assert(pos != end());Node* cur = pos._node;Node* nextNode = cur->_next;Node* prevNode = cur->_prev;prevNode->_next = nextNode;nextNode->_prev = prevNode;delete cur;--_size;return iterator(nextNode);}private:Node* _head;size_t _size;};
}

2. list 的反向迭代器

        反向迭代器的 ++ 就是正向迭代器的 --,反向迭代器的 -- 就是正向迭代器的 ++。因此,反向迭代器的实现可以借助正向迭代器,即:反向迭代器内部可以包含一个正向迭代器,对正向迭代器的接口进行包装即可。

// Iterator.h
// 所有容器的反向迭代器
// 迭代器适配器
#pragma oncetemplate<class Iterator, class Ref, class Ptr>
class ReverseIterator
{
public:typedef ReverseIterator<Iterator, Ref, Ptr> Self;ReverseIterator(Iterator it): _it(it){}Ref operator*(){//return *_it;	// 第一种写法Iterator tmp = _it;--tmp;return *tmp;}Ptr operator->(){return &(operator*());}Self& operator++(){--_it;return *this;}Self& operator--(){++_it;return *this;}bool operator!=(const Self& s){return _it != s._it;}bool operator==(const Self& s){return _it == s._it;}private:Iterator _it;
};

3. list 与 vector 的对比

        vector 与 list 都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不同,其主要不同如下:

vectorlist
底层结构动态顺序表,一段连续空间带头结点的双向循环链表
随机访问支持随机访问,访问某个元素效率O(1)不支持随机访问,访问某个元素效率O(n)
插入和删除任意位置插入和删除效率低,需要挪动元素,时间复杂度为O(n),插入时有可能需要扩容,扩容:开辟新空间,拷贝元素,释放旧空间,导致效率更低任意位置插入和删除效率高,不需要挪动元素,时间复杂度为O(1)
空间利用率底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高底层结点动态开辟,小结点容易造成内存碎片,空间利用率低,缓存利用率低
迭代器原生态指针对原生态指针(结点指针)进行封装
迭代器失效在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响
使用场景需要高效存储,支持随机访问,不关心插入删除效率大量插入和删除操作,不关心随机访问

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

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

相关文章

边缘计算:一场由物理定律发起的“计算革命”

专栏引言:在前面的文章中,我们探讨了云计算如何将计算资源变成了“数字水电煤”,构建了一个强大的中心化数字帝国。然而,当这个帝国试图将它的触角伸向物理世界的每一个角落时,却遭遇了两位“上古之神”的无情阻击——光速与带宽。今天,我们将聚焦于一场由物理定律发起的…

量化模型部署工具llama.cpp

量化模型部署工具llama.cppllama.cppllama.cpp 是什么使用场景是什么如何使用&#xff1f;第 1 步&#xff1a;获取量化模型第 2 步&#xff1a;编译 llama.cpp第 3 步&#xff1a;运行推理完整 Demo&#xff1a;与 Llama 3 对话进阶使用&#xff1a;Python 集成总结概念解释1.…

【光照】[光照模型]发展里程碑时间线

【从UnityURP开始探索游戏渲染】专栏-直达 图形学光照模型发展史&#xff1a;技术演进与里程碑 section 基础奠基期(1960s-1970s) 1967 &#xff1a; Lambert模型(漫反射) - Bui Tuong Phong提出1971 &#xff1a; Gouraud着色 - Henri Gouraud发明顶点插值着色1973 &#xf…

【从零开始java学习|第十篇】面向对象

目录 一、面向对象介绍 二、类和对象 1. 类&#xff08;Class&#xff09;&#xff1a;对象的模板 2. 对象&#xff08;Object&#xff09;&#xff1a;类的实例 三、封装 1. 封装的概念 2. 封装的优势 四、就近原则和 this 关键字 1. 就近原则 2. this 关键字 五、…

Spark算子调优

Spark中可用下面的算子对数据计算进行优化处理&#xff0c;包括&#xff1a; mapPartition&#xff1a;一次处理一个分区数据&#xff0c;能够使用mapPartition的尽量使用&#xff0c;但是使用时会一次性读取整个分区数据到内存&#xff0c;占内存很大&#xff0c;同理还有fore…

码农特供版《消费者权益保护法》逆向工程指北——附源码级注释与异常处理方案

尊敬的审核&#xff1a; 本人文章《码农特供版〈消费者权益保护法〉逆向工程指北——附源码级注释与异常处理方案》 1. 纯属技术交流&#xff0c;无任何违法内容 2. 所有法律引用均来自公开条文 3. 请依据《网络安全法》第12条“不得无故删除合法内容”处理 附&#xff1a;本文…

MQTT 连接建立与断开流程详解(二)

三、核心机制与最佳实践&#xff08;一&#xff09;会话管理与 QoS 保障Clean Session vs 持久会话&#xff1a;在 MQTT 连接中&#xff0c;会话管理是一个重要的概念&#xff0c;其中 Clean Session 和持久会话是两种不同的会话模式。Clean Session&#xff0c;当设置为 1 时&…

[光学原理与应用-332]:ZEMAX - 序列模式与非序列模式的本质、比较

序列模式&#xff08;Sequential Mode&#xff09;与非序列模式&#xff08;Non-Sequential Mode&#xff09;是ZEMAX光学设计软件中的两种核心设计模式&#xff0c;二者在光路定义、分析工具、应用场景等方面存在本质差异。以下是两者的详细比较&#xff1a;一、本质差异光路定…

WeakAuras Lua Script (My Version)

分享下我的WA的简约配置&#xff0c;大多数都是团队框架高亮&#xff0c;辅助大脚DBM监控 表格&#xff1a; WeakAuras Lua Script &#xff1c;BiaoGe&#xff1e;_wa拍卖字符串-CSDN博客 ICC 监控&#xff0c;只要团队框架监控 WeakAuras Lua Script ICC &#xff08;Barne…

【Python+requests】解决Python requests中的ProxyError:SSL版本错误问题详解

解决Python requests中的ProxyError&#xff1a;SSL版本错误问题详解 在使用Python进行网络请求时&#xff0c;很多人都会用到requests库配合代理服务器进行调试或抓包。但有时会遇到令人困惑的ProxyError&#xff0c;尤其是伴随SSLError: [SSL: WRONG_VERSION_NUMBER]这样的错…

基于deepseek的Spring boot入门

一次跟着deepseek记笔记的尝试&#xff0c;由于CSDN没有思维导图&#xff0c;只能按层级记录提问 如果我想知道一个springboot项目的基本结构&#xff0c;比如用到了哪些组件&#xff0c;入口在哪&#xff0c;数据库配置是怎样的 应该从哪里开始 springboot有哪些常用注解 一个…

macOS 15.6 ARM golang debug 问题

前言 最近使用macmini m4在使用golang debug发现一些奇怪的问题&#xff0c;debug到c代码&#xff0c;莫名其妙&#xff0c;而且不知道什么原因&#xff0c;知道搜索查询&#xff0c;才发现是苹果的Command Line Tools 的锅&#xff0c;macOS 15果然是一堆bug&#xff0c;毕竟…

有个需求:切换车队身份实现Fragment的Tab隐藏显示(车队不显示奖赏)

核心实现&#xff1a; 1使用mmkv保存切换的身份 2借助eventbus实现通知Fragment的tab更新private void switchFleet(boolean isMore, EnterpriseInfo enterpriseInfo) {if (isMore) {tvSwitchFleetTitle.setText(getText(R.string.switch_to_other_accounts));} else {tvSwitch…

在 Android Studio 中修改 APK 启动图标(2025826)

在 Android Studio 中修改 Android 12 应用图标可以按照以下步骤进行&#xff1a;1、准备图标资源准备一个启动图标&#xff08;建议使用 SVG 格式或高分辨率 PNG&#xff0c;推荐尺寸为 512x512 像素&#xff09;图标应符合 Android 12 的设计规范&#xff08;自适应图标&…

Linux三剑客grep-sed-awk

linux三剑客-grep、sed、awk 文章目录linux三剑客-grep、sed、awk1.正则表达式1.1正则表达式&#xff1f;1.2应用场景&#xff1f;-谁可以用&#xff1f;1.3正则注意事项&#xff08;避免90%以上的坑&#xff09;1.4正则符号1.5正则VS通配符2.基础正则2.1 ^ 以...开头的行2.2 $…

给某个conda环境安装CUDA 12.4版本 全局CUDA不变

文章目录&#x1f3af; 正确的安装命令&#x1f4cb; 为什么这个命令安全&#xff1f;⚠️ 重要说明&#x1f527; 安装后环境配置方法一&#xff1a;在虚拟环境中设置方法二&#xff1a;使用conda环境变量&#x1f9ea; 验证安装&#x1f4ca; 版本共存确认&#x1f4a1; 额外…

【C++】日期类实现详解:代码解析与复用优化

代码总览与总结// 实现一个完善的日期类 class Date { public:int GetMonthDay(int year, int month){int monthDays[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };// 闰年2月if (month 2 && ((year % 4 0 && year % 100 ! 0) || year % 400 0))return …

零基础json入门教程(基于vscode的json配置文件)

一、什么是 JSON&#xff1f;—— 最核心的概念JSON&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0c;长得像键值对的集合&#xff0c;用途是存储和传递数据。在 VS Code 中&#xff0c;它常被用来写配置文件&#xff08;比如调试配置…

深入对比分析SpringCloud和Dubbo两大微服务框架的核心架构与特性。

1 微服务架构演进与核心概念1.1 微服务架构的本质微服务架构是一种将单一应用程序划分为一组小型服务的方法&#xff0c;每个服务运行在自己的进程中&#xff0c;服务之间通过轻量级的通信机制进行协作。这些服务围绕业务能力构建&#xff0c;并能够独立部署到生产环境中。微服…

鸿蒙ArkTS 核心篇-14-条件表达式(三目运算符)

目录 根据逻辑条件结果&#xff0c;执行不同的表达式&#xff0c;得到不同结果 DevEco Studio代码实战 ctrl s 日志输出 总结 根据逻辑条件结果&#xff0c;执行不同的表达式&#xff0c;得到不同结果 DevEco Studio代码实战 let num1: number 100 let num2: number 20l…