当前位置: 首页 > news >正文

C++:模拟实现list及迭代器类模板优化方法

文章目录

  • 迭代器
  • 模拟实现

本篇模拟实现简单的list和一些其他注意的点

在这里插入图片描述

迭代器

如下所示是利用拷贝构造将一个链表中的数据挪动到另外一个链表中,构造两个相同的链表

list(const list<T>& lt)
{emptyinit();for (auto e : lt){push_back(e);}
}void test_list1()
{list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_back(5);list<int> l2(lt);
}

lt作为形参,传引用可以提高传参的效率,同时应该加上const修饰来保证不会因为不小心修改了形参导致外部的实参也被修改,这样的结果是不应该的,因此就要加const把这个形参变成一个const对象

而问题又来了,const对象要使用的是const迭代器,而前面没有写const迭代器,因此这里就引入了const迭代器的实现

从vector的模拟实现中,看似似乎只需要在原来的迭代器的基础上加上一个const即可,但事实上:

const迭代器和普通迭代器是两种不同的迭代器,不能直接在普通的迭代器后面加const,原因?

要解决这个问题,先重新回顾一下vector中const迭代器的定义流程:

对比vector的迭代器,vector中的迭代器const版本和非const版本直接在非const版本后面加const使它变成const迭代器即可,这样在调用的时候就可以直接进行调用

在这里插入图片描述

iterator的定义,const版本就是在指针前面加上const,这样返回的就是const修饰的指针,因此就可以做到通过该迭代器只读,不可修改的作用

在这里插入图片描述

这里的迭代器本质上就是指针在底层进行访问,然后我们定义一个const指针,使得const指针就不能对指针指向的内容进行修改了

下面仿照vector修改的原理修改list

要修改的部分其实就是下面的代码:

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

在函数后加const很简单,但是核心是要把返回值也定义为const指针版本,这个过程要如何实现?

由于这里是把iterator封装成了一个类进行的实现,因此需要重新封装一个类进行实现

	template <class T>struct __list_iterator{typedef list_node<T> Node;typedef  __list_iterator<T> self;// 成员Node* _node;__list_iterator(Node* node):_node(node){}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;}T& operator*(){return _node->_data;}T* operator->(){return &_node->_data;}bool operator==(const self& pos){return _node == pos._node;}bool operator!=(const self& pos){return !(*this == pos);}};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){}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& pos){return _node == pos._node;}bool operator!=(const self& pos){return !(*this == pos);}};

但事实上,这两份代码只有很少的地方有区别,更多的内容都是相同的,这样是不满足较好的代码风格,因此在stl源码中,使用了模板对这两个类进行了封装

改进版本如下:

	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){}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;}Ptr operator->(){return &_node->_data;}bool operator==(const self& pos){return _node == pos._node;}bool operator!=(const self& pos){return !(*this == pos);}};template <class T>class list{void emptyinit(){_head = new Node();_head->_next = _head;_head->_prev = _head;_size = 0;}public:typedef list_node<T> Node;typedef __list_iterator<T, T&, T*> iterator;typedef __list_iterator<T, const T&, const T*> const_iterator;// 构造函数 list(){emptyinit();}// 拷贝构造list(const list<T>& lt){emptyinit();auto it = lt.begin();//*it = 30;}void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}// head     tail->prev  tailvoid pop_back(){erase(--end());}void pop_front(){erase(begin());}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);}iterator insert(iterator pos, const T& x){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);//        newnode//   prev         curprev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;return iterator(newnode);}iterator erase(iterator pos){Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;// prev cur nextprev->_next = cur->_next;next->_prev = cur->_prev;return iterator(next);}private:Node* _head;size_t _size;};

这里引入了类模板的概念,简单来说,当需要调用const类型时就会模板实例化出一个const版本的迭代器,再进行相关的操作,这样的操作可以避免代码冗余,也是模板的强大之处

模拟实现

#pragma once// 实现的是双向循环链表,链表中应该包含节点类和迭代器类,节点类用于从内存中申请节点,迭代器类用于获取节点指针
namespace mylist
{// 把节点进行封装,可以动态获取一个节点template <class T>struct list_node{// 成员list_node<T>* _next;list_node<T>* _prev;T _data;// 成员函数list_node(const T& val = T()):_data(val), _next(nullptr), _prev(nullptr){}};// 对迭代器进行封装,使得外界看到的是迭代器// 迭代器当中存储的是某个节点的指针// 可以对迭代器进行各项操作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){}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;}Ptr operator->(){return &_node->_data;}bool operator==(const self& pos){return _node == pos._node;}bool operator!=(const self& pos){return !(*this == pos);}};// 适配器 -- 复用template <class T, class Ref, class Ptr >struct __reverse_iterator{typedef list_node<T> Node;typedef  __reverse_iterator<T, Ref, Ptr> self;// 成员Node* _node;__reverse_iterator(Node* node):_node(node){}self& operator++(){_node = _node->_prev;return *this;}self& operator--(){_node = _node->_next;return *this;}self operator++(int){self tmp(*this);_node = _node->_prev;return tmp;}self operator--(int){self tmp(*this);_node = _node->_next;return tmp;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}bool operator==(const self& pos){return _node == pos._node;}bool operator!=(const self& pos){return !(*this == pos);}};template <class T>class list{void emptyinit(){_head = new Node();_head->_next = _head;_head->_prev = _head;_size = 0;}public:typedef list_node<T> Node;typedef __list_iterator<T, T&, T*> iterator;typedef __list_iterator<T, const T&, const T*> const_iterator;typedef __reverse_iterator<T, T&, T*> reverse_iterator;typedef __reverse_iterator<T, const T&, const T*> const_reverse_iterator;// 构造函数 list(){emptyinit();}// 拷贝构造list(const list<T>& lt){emptyinit();auto it = lt.begin();//*it = 30;}void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}// head     tail->prev  tailvoid pop_back(){erase(--end());}void pop_front(){erase(begin());}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);}reverse_iterator rbegin(){return reverse_iterator(_head->_prev);}reverse_iterator rend(){return reverse_iterator(_head);}const_reverse_iterator rbegin() const{return const_reverse_iterator(_head->_prev);}const_reverse_iterator rend() const{return const_reverse_iterator(_head);}iterator insert(iterator pos, const T& x){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);//        newnode//   prev         curprev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;return iterator(newnode);}iterator erase(iterator pos){Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;// prev cur nextprev->_next = cur->_next;next->_prev = cur->_prev;return iterator(next);}private:Node* _head;size_t _size;};void test_list1(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_back(5);cout << "正序" << endl;for (auto e : lt){cout << e << " ";}cout << endl;cout << "逆序" << endl;auto rit = lt.rbegin();while (rit != lt.rend()){cout << *rit << " ";rit++;}cout << endl;}
}

相关文章:

C++:模拟实现list及迭代器类模板优化方法

文章目录 迭代器模拟实现 本篇模拟实现简单的list和一些其他注意的点 迭代器 如下所示是利用拷贝构造将一个链表中的数据挪动到另外一个链表中&#xff0c;构造两个相同的链表 list(const list<T>& lt) {emptyinit();for (auto e : lt){push_back(e);} }void test_…...

k8s整合istio配置gateway入口、配置集群内部服务调用管理

一、 istio gateway使用demo kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata:name: ngdemo-gatewaynamespace: ssx spec:selector:istio: ingressgateway # use Istio default gateway implementationservers:- port:numbe…...

工程监测振弦采集仪采集到的数据如何进行分析和处理

工程监测振弦采集仪采集到的数据如何进行分析和处理 振弦采集仪是一个用于测量和记录物体振动的设备。它通过测量物体表面的振动来提取振动信号数据&#xff0c;然后将其转换为数字信号&#xff0c;以便进行分析和处理。在实际应用中&#xff0c;振弦采集仪是广泛应用于机械、建…...

(三)行为模式:2、命令模式(Command Pattern)(C++示例)

目录 1、命令模式&#xff08;Command Pattern&#xff09;含义 2、命令模式的UML图学习 3、命令模式的应用场景 4、命令模式的优缺点 5、C实现命令模式的实例 1、命令模式&#xff08;Command Pattern&#xff09;含义 命令模式&#xff08;Command&#xff09;&#xff…...

微信小程序 蓝牙设备连接,控制开关灯

1.前言 微信小程序中连接蓝牙设备&#xff0c;信息写入流程 1、检测当前使用设备&#xff08;如自己的手机&#xff09;是否支持蓝牙/蓝牙开启状态 wx:openBluetoothAdapter({}) 2、如蓝牙已开启状态&#xff0c;检查蓝牙适配器的状态 wx.getBluetoothAdapterState({}) 3、添加…...

Python 矢量数据库和矢量索引:构建 LLM 应用程序

推荐&#xff1a;使用 NSDT场景编辑器 助你快速搭建可二次编辑的3D应用场景 由于使用其硬件创建的生成式AI应用程序&#xff0c;Nvidia经历了显着的增长。另一项软件创新&#xff0c;矢量数据库&#xff0c;也正在乘着生成式人工智能的浪潮。 开发人员正在向量数据库上用Pytho…...

-Webkit-Box 在 Safari 中出现的兼容性问题

一、问题背景&#xff1a; UI要求要实现这样的效果&#xff0c;使用 display:-webket-box在chrome浏览器下完美解决 但是马上啪啪打脸&#xff0c;在safari浏览器下显示空白 &#xff0c;不能不说浏览器之间的兼容性简直就是天坑 二、解决办法 通过浏览器调试发现原本float的…...

后端项目打包上传服务器记录

后端项目打包上传服务器记录 文章目录 后端项目打包上传服务器记录1、项目打包2、jar包上传服务器 本文记录打包一个后端项目&#xff0c;上传公司服务器的过程。 1、项目打包 通过IDEA的插件进行打包&#xff1a; 打成一个jar包&#xff0c;jar包的位置在控制台可以看到。 2、…...

ubuntu部署haproxy

HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理. 1、更新系统报 通过在终端中运行以下命令&#xff0c;确保所有系统包都是最新的 sudo apt updatesudo apt upgrade2、安装Haproxy sudo apt install haproxy设置开机自动启动haproxy服务 sudo systemctl en…...

vue利用 sortable 完成表格拖拽

先讲一下vue2&#xff0c;使用sortable完成表格拖拽【不只是表格&#xff0c;div也可以实现&#xff0c;但我项目中是表格拖拽】 github地址 安装 npm install sortablejs --save使用 &#xff08;我的项目中是拖拽一个小按钮移动&#xff0c;而不是整行&#xff09; <te…...

CNN简介3

...

新能源电动车充电桩控制主板安全特点

新能源电动车充电桩控制主板安全特点 你是否曾经担心过充电桩的安全问题?充电桩主板又是什么样的呢?今天我们就来聊聊这个话题。 充电桩主板采用双重安全防护系统&#xff0c;包括防水、防护、防尘等&#xff0c;确保充电桩安全、可靠。不仅如此&#xff0c;充电桩主板采用先…...

公路桥梁有哪些安全隐患?

在现代社会&#xff0c;公路桥梁作为连接城市、串联交通的重要纽带&#xff0c;扮演着无可替代的角色。然而&#xff0c;我们常常忽视的是&#xff0c;这些高架构筑物也存在着潜在的安全隐患&#xff0c;可能随时影响着交通的畅通和人们的生命财产安全。为了更好地认识和理解这…...

【C语言】每日一题(错误的集合)

最近在牛客、力扣上做题&#xff0c;花费海量时间&#xff0c;苦不堪言&#xff0c;有时绞尽脑汁也想不出&#xff0c;痛定思痛&#xff0c;每日记录写的比较困难的题。 错误的集合 题目如上图所示 题主乍看之下觉得很简单&#xff0c;再看例子&#xff0c;不就是一个有序数组…...

[JavaWeb]【四】web后端开发-SpringBootWeb入门

目录 一 Spring 二 SpringBootWeb入门 2.1 入门需求 2.2 分析 2.3 开始创建SpringBootWeb 2.4 创建类实现需求 2.5 启动程序 2.6 访问 三 HTTP协议 3.1 HTTP-概述 3.2 HTTP-请求协议 3.3 HTTP-响应协议 3.3.1 响应状态码 && 响应类型 3.4 HTTP-协议解析 前言…...

前端css

day03-CSS基础 目标&#xff1a;掌握 CSS 属性基本写法&#xff0c;能够使用文字相关属性美化文章页。 01-CSS初体验 层叠样式表 (Cascading Style Sheets&#xff0c;缩写为 CSS&#xff09;&#xff0c;是一种 样式表 语言&#xff0c;用来描述 HTML 文档的呈现&#xff08;…...

vb+sql医院门诊管理系统设计与系统

摘要 信息时代已经来临,计算机应用于医院的日常管理,为医院的现代化带来了从未有过的动力和机遇,为医疗卫生领域的发展提供了无限的潜力。采用计算机管理信息系统已成为医院管理科学化和现代化的标志,给医院带来了明显的经济效益和社会效益。 本文介绍了数据库管理系统的…...

bootstrap-modal调用ajax后不经过回调函数

说明&#xff1a;我用的是boostrap的弹框&#xff0c;表单用的是layui的&#xff0c;个人觉得bootstrap比layui的弹框好看点&#xff0c;能自适应高度。 如图&#xff1a;点击保存后里面的内容不执行 原因&#xff1a;type用的是submit 解决&#xff1a;把submit改为button...

【【典型电路设计之片内存储器的设计之RAM的Verilog HDL描述一】】

典型电路设计之片内存储器的设计之RAM的Verilog HDL描述一 RAM是随机存储器&#xff0c;存储单元的内容可按需随意取出或存入。这种存储器在断电后将丢失所有数据&#xff0c;一般用来存储一些短时间内使用的程序和数据。 其内部结构如下图所示&#xff1a; 例&#xff1a;用…...

食品行业案例 | 燕千云助力头部食品企业搭建数智化 IT服务管理体系及平台

随着数字化时代的到来&#xff0c;食品行业呈现出多个发展趋势。首先&#xff0c;消费者对健康食品和功能性食品的关注度提高&#xff0c;推动了市场需求的增长。其次&#xff0c;便利食品和物流行业迅速发展&#xff0c;满足了快节奏生活的需求。再者&#xff0c;电商渠道和网…...

2026指纹浏览器风控对抗技术实践:从特征伪装到行为合规的全流程落地

一、引言&#xff1a;多账号运营场景下的风控挑战与技术诉求随着 2026 年全球互联网平台风控技术的持续迭代&#xff0c;AI 驱动的多维度交叉验证已成为主流风控模式&#xff0c;平台不仅对设备硬件指纹、网络环境进行深度检测&#xff0c;更将操作行为、业务数据、行为轨迹纳入…...

超图神经网络(HGNN)实战:从多模态数据构建到节点特征提取

超图神经网络实战&#xff1a;多模态数据融合与节点特征提取全流程解析 在电商推荐系统中&#xff0c;我们常常需要同时处理用户行为日志、商品图像和评论文本——这三种异构数据如何统一建模&#xff1f;传统图神经网络&#xff08;GNN&#xff09;在处理这类多模态关联时往往…...

保姆级教程:在ROS2 Humble和Gazebo 11中配置FAST_LIO_ROS2进行三维SLAM仿真

从零搭建ROS2与Gazebo环境&#xff1a;FAST_LIO_ROS2三维SLAM实战指南 刚接触机器人仿真的开发者常被环境配置的复杂性劝退——依赖冲突、参数配置错误、话题不匹配等问题层出不穷。本文将手把手带您完成ROS2 Humble、Gazebo 11与FAST_LIO_ROS2的完整集成&#xff0c;实现一个可…...

别再被AI骗了,这个分层检索让它不得不诚实

大语言模型能写出流畅的文章&#xff0c;却经常“一本正经地胡说八道”——即所谓的"幻觉"。本文提出了一种"领域感知分层检索"架构&#xff0c;通过四阶段自调节管道&#xff0c;将LLM从"概率猜测者"转变为"事实验证者"。下图为幻觉分…...

springboot-vue+nodejs 的学生请假销假管理系统

目录技术选型与架构设计系统模块划分审批流程实现销假与统计功能系统集成与部署安全与性能优化项目技术支持源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作技术选型与架构设计 后端采用SpringBoot框架&#xff0c;提供RESTful API接口&…...

别再只会用按钮上传了!用JEECG的JUpload组件打造更优雅的后台文件管理界面

从按钮到拖拽&#xff1a;用JEECG的JUpload组件重构后台文件管理体验 在后台管理系统开发中&#xff0c;文件上传功能几乎是每个项目都无法绕开的刚需。但你是否注意到&#xff0c;大多数开发者仍然停留在传统的按钮式上传方式&#xff1f;这种"点击-选择-上传"的三部…...

实战演练:基于Spring Boot的个人博客系统,用快马AI一键生成完整后端代码

最近在尝试搭建一个个人博客系统&#xff0c;正好用Spring Boot练练手。作为一个Java开发者&#xff0c;我发现用InsCode(快马)平台可以快速生成完整的后端代码&#xff0c;省去了很多重复劳动。下面分享下我的实战经验&#xff1a; 项目初始化 首先明确需求&#xff0c;博客系…...

OpenClaw隐私保护方案:Qwen3-32B本地推理的医疗数据处理

OpenClaw隐私保护方案&#xff1a;Qwen3-32B本地推理的医疗数据处理 1. 为什么医疗数据需要本地化AI处理 去年参与一个医疗数据分析项目时&#xff0c;我首次意识到数据隐私的严峻性。客户提供的患者诊疗记录包含身份证号、住址和病史等敏感信息&#xff0c;而团队最初考虑使…...

Open WebUI:企业级自托管AI平台架构深度解析

Open WebUI&#xff1a;企业级自托管AI平台架构深度解析 【免费下载链接】open-webui Open WebUI 是一个可扩展、功能丰富且用户友好的自托管 WebUI&#xff0c;设计用于完全离线操作&#xff0c;支持各种大型语言模型&#xff08;LLM&#xff09;运行器&#xff0c;包括Ollama…...

别再死磕Release了!用Debug模式打包Qt exe,一次搞定所有dll依赖(附GPT脚本生成技巧)

用Debug模式打包Qt应用&#xff1a;绕过Release陷阱的高效解决方案 当Qt开发者第一次尝试将项目打包成可独立运行的exe文件时&#xff0c;绝大多数教程都会推荐使用Release模式编译。然而在实际操作中&#xff0c;Release模式可能会带来一系列难以排查的问题——从莫名其妙的崩…...