【C++精简版回顾】21.迭代器,实现迭代器

1.什么是迭代器?

        用来遍历容器,访问容器数据。

2.迭代器使用

1.初始化

//初始化
list<int> mylist;//list的整数对象
list<int>::iterator iter;//list内部类,迭代器对象(正向输出)
list<int>::reverse_iterator riter;//list内部类,迭代器对象(反向输出)
int array[5] = { 1,2,3,4,5 };

2.添加数据

//添加数据到list中
mylist.assign(array, array + 5);

3.正向输出

//正向输出
for (iter = mylist.begin();iter != mylist.end();iter++) {
	cout << *iter << "\t" ;
}

4.反向输出

for (riter = mylist.rbegin();riter != mylist.rend();riter++) {
	cout << *riter << "\t";
}

结果:

 3.实现一个反向迭代器

1.建立一个节点(用来存储数据)

template<class type>
struct Node {
	type data;
	Node<type>* left;
	Node<type>* right;
	Node(type data):data(data),left(nullptr),right(nullptr){}
	Node(type data, Node<type>*left, Node<type>*right) :data(data) ,left(left),right(right){}
};

2.事项简单的list类(类中包括一个反方向的迭代器)

template<class type>
class list {
public:
	list() {}
	//因为需要反向迭代,所以需要建立双链表
	void assign(type* begin,type* end) {
		while (begin != end) {
			Node<type>* node = new Node<type>(*begin);
			if (size == 0) {
				listhead = node;
				listend = node;
				node->left = node;
				node->right = node;
			}
			else {
				listend->right = node;
				node->right = listhead;
				node->left = listend;
				listend = node;
				listhead->left = node;				
			}
			size++;
			begin++;
		}
	}
	//rbegin,rend
	Node<type>* rbegin() {
		return listend;
	}
	Node<type>* rend() {
		return listhead;
	}
//反方向的迭代器-----------------------------------------
	class reverse_iterator {
	public:
		void operator=(Node<type>* node) {
			this->pmove = node;
		}
		bool operator!=(Node<type>* node) {
			return this->pmove != node;
		}
		reverse_iterator& operator++(int) {
			this->pmove = this->pmove->left;
			return (*this);
		}
		type operator*() {
			return this->pmove->data;
		}
	protected:
		Node<type>* pmove;
	};
protected:
	Node<type>* listhead;
	Node<type>* listend;
	int size=0;
};

3.对上述代码的解释

(1)插入代码,普通指针指向修改

	//因为需要反向迭代,所以需要建立双链表
	void assign(type* begin,type* end) {
		while (begin != end) {
			Node<type>* node = new Node<type>(*begin);
			if (size == 0) {
				listhead = node;
				listend = node;
				node->left = node;
				node->right = node;
			}
			else {
				listend->right = node;
				node->right = listhead;
				node->left = listend;
				listend = node;
				listhead->left = node;				
			}
			size++;
			begin++;
		}
	}

(2)单目运算符的重载一般在类中直接实现,使用友元函数会增加传参

class reverse_iterator {
	public:

		void operator=(Node<type>* node) {
			this->pmove = node;
		}

		bool operator!=(Node<type>* node) {
			return this->pmove != node;
		}

		reverse_iterator& operator++(int) {
			this->pmove = this->pmove->left;
			return (*this);
		}

		type operator*() {
			return this->pmove->data;
		}
	protected:
		Node<type>* pmove;
	};

结果:

整个代码: 瑕疵之处

       1.析构函数没有使用,目前不太清楚应该在哪里开始析构。

        2.数据1输出不出来,因为下面代码在头节点会报0。

解决方法:使用一个头节点(不存放数据)

bool operator!=(Node<type>* node) {
	return this->pmove != node;
}

附上总体代码

1.main

#include"标头.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
	//list链表 反向输出 迭代器
	list<int>::reverse_iterator riter;
	//list<int> mylist = { 1,2,3,4,5,6 };
	int arr[6] = { 1,2,3,4,5,6 };
	list<int> mylist;
	mylist.assign(arr, arr + 6);
	for (riter = mylist.rbegin();riter != mylist.rend();riter++) {
		cout << *riter << "\t";
	}
	cout << endl;
	return 0;
}

2.实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class type>
struct Node {
	type data;
	Node<type>* left;
	Node<type>* right;
	Node(type data):data(data),left(nullptr),right(nullptr){}
	Node(type data, Node<type>*left, Node<type>*right) :data(data) ,left(left),right(right){}
};
template<class type>
class list {
public:
	list() {}
	//因为需要反向迭代,所以需要建立双链表
	void assign(type* begin,type* end) {
		while (begin != end) {
			Node<type>* node = new Node<type>(*begin);
			if (size == 0) {
				listhead = node;
				listend = node;
				node->left = node;
				node->right = node;
			}
			else {
				listend->right = node;
				node->right = listhead;
				node->left = listend;
				listend = node;
				listhead->left = node;				
			}
			size++;
			begin++;
		}
	}
	//rbegin,rend
	Node<type>* rbegin() {
		return listend;
	}
	Node<type>* rend() {
		return listhead;
	}
	class reverse_iterator {
	public:
		void operator=(Node<type>* node) {
			this->pmove = node;
		}
		bool operator!=(Node<type>* node) {
			return this->pmove != node;
		}
		reverse_iterator& operator++(int) {
			this->pmove = this->pmove->left;
			return (*this);
		}
		type operator*() {
			return this->pmove->data;
		}
	protected:
		Node<type>* pmove;
	};
protected:
	Node<type>* listhead;
	Node<type>* listend;
	int size=0;
};

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

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

相关文章

华为配置ISP选路实现报文按运营商转发

Web举例&#xff1a;配置ISP选路实现报文按运营商转发 介绍通过配置ISP选路实现报文按运营商转发的配置举例。 组网需求 如图1所示&#xff0c;FW作为安全网关部署在网络出口&#xff0c;企业分别从ISP1和ISP2租用一条链路。 企业希望访问Server 1的报文从ISP1链路转发&#…

把 Windows 装进 Docker 容器里

本篇文章聊聊如何在 Docker 里运行 Windows 操作系统&#xff0c; Windows in Docker Container&#xff08;WinD&#xff09;。 写在前面 我日常使用 macOS 和 Ubuntu 来学习和工作&#xff0c;但是时不时会有 Windows 使用的场景&#xff0c;不论是运行某个指定的软件&…

记录一下某外资的面试

文章目录 标题English introduction标题What did u do in this gap time标题What’S the big challenge in your work experience标题 4、介绍一个自己熟悉的项目或最近的项目&#xff0c;包括项目的背景&#xff0c;使用的技术&#xff0c;在里面的角色标题5、项目中有多少个微…

Java中 常见的开源图库介绍

阅读本文之前请参阅------Java中 图的基础知识介绍 在 Java 中&#xff0c;有几种流行的开源图库&#xff0c;它们提供了丰富的图算法和高级操作&#xff0c;可以帮助开发者更高效地处理图相关的问题。以下是几种常见的 Java 图库及其特点和区别&#xff1a; JGraphT …

Github 2024-03-11 开源项目周报 Top15

根据Github Trendings的统计,本周(2024-03-11统计)共有15个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量Python项目4TypeScript项目3Jupyter Notebook项目3C#项目1HTML项目1CSS项目1Dart项目1Lua项目1Shell项目1Rust项目1Java项目1C++项目1屏幕截图转…

使用 JCommander 解析命令行参数

前言 如果你想构建一个支持命令行参数的程序&#xff0c;那么 jcommander 非常适合你&#xff0c;jcommander 是一个只有几十 kb 的 Java 命令行参数解析工具&#xff0c;可以通过注解的方式快速实现命令行参数解析。 这篇教程会通过介绍 jcommadner &#xff0c;快速的创建一…

TransNeXt实战:使用TransNeXt实现图像分类任务(一)

文章目录 摘要安装包安装timm 数据增强Cutout和MixupEMA项目结构计算mean和std生成数据集 摘要 https://arxiv.org/pdf/2311.17132.pdf TransNeXt是一种视觉骨干网络&#xff0c;它集成了聚合注意力作为令牌混合器和卷积GLU作为通道混合器。通过图像分类、目标检测和分割任务…

使用nexus3搭建npm私有仓库

一、下载解压安装包 下载地址&#xff1a;nexus-3.66.0-02-win.zip 二、安装并运行私服 在cmd中进入到文件夹中的bin目录下运行一下命令 nexus.exe /run等几分钟启动后&#xff0c;浏览器访问&#xff1a;默认端口8081 http://127.0.0.1:8081/ #修改端口在etc文件下 nexus-…

ORACLE Linux(OEL) - Primavera P6EPPM 安装及分享

引言 继上一期发布的CentOS版环境发布之后&#xff0c;近日我制作了基于ORACLE Linux的P6虚拟机环境&#xff0c;同样里面包含了全套P6 最新版应用服务 此虚拟机仅用于演示、培训和测试目的。如您在生产环境中使用此虚拟机&#xff0c;请先与Oracle Primavera销售代表取得联系…

凌鲨本地接口架构

本地API通过监听本地端口&#xff0c;提供http服务&#xff0c;让本地应用可以获取信息和操作凌鲨客户端。 本地API架构 #mermaid-svg-seodZa6VsI4Qc8Cj {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-seodZa6VsI4…

联想小新电脑出现蓝屏问题解决(暂时没有解决)

电脑出现蓝屏&#xff0c;如下 搜索FAULTY_HARDWARE_CORRUPTED_PAGE寻找解决方案&#xff0c;找到较为靠谱的文章&#xff1a;记录蓝屏问题FAULTY_HARDWARE_CORRUPTED_PAGE 根据文章提示找到官方解答&#xff1a;Bug 检查 0x12B&#xff1a;FAULTY_HARDWARE_CORRUPTED_PAGE&…

基于Android的高校移动成绩查询系统的设计与实现

摘 要 在我国现今状态&#xff0c;互联网呈现出的高速发展状态以及高等教育的教学不断改革下&#xff0c;各高校的教务管理系统都已经从传统的纸质方式转向了基于Internet的绿色管理方式。而对于目前各高校所使用的都是浏览器/服务器&#xff08;B/S&#xff09;模式&#xff…

Servlet的book图书表格实现(使用原生js实现)

作业内容&#xff1a; 1 建立一个book.html,实现图书入库提交 整体参考效果如下: 数据提交后&#xff0c;以窗口弹出数据结果&#xff0c;如: 2 使用正则表达式验证ISBN为x-x-x格式&#xff0c;图书名不低于2个字符&#xff0c;作者不能为空&#xff0c;单价在【10-100】之间…

【解读】区块链和分布式记账技术标准体系建设指南

大家好&#xff0c;这里是苏泽。一个从业Java后端的区块链技术爱好者。 今天带大家来解读这份三部门印发的行业建设指南《区块链和分布式记账技术标准体系建设指南》 原文件可查看P020240112840724196854.pdf (www.gov.cn) 以下是个人解读&#xff0c;如有纰漏请指正&#xff…

无人机手持地面站软件功能详解,无人机手持地面站软件开发人员组成及成本分析

无人机手持地面站软件是专为无人机操控和任务管理设计的移动应用&#xff0c;它通常集成在智能手机、平板电脑或其他便携式设备上&#xff0c;使得用户可以在远离无人机的地方对飞行器进行实时监控与远程控制。 主要功能详解&#xff1a; 1. 飞行控制与姿态显示&#xff1a; …

深度学习:生成模型的创新应用与未来展望,引领科技新潮流!

在人工智能的浪潮中&#xff0c;深度学习正以其强大的学习和表示能力&#xff0c;不断推动着各个领域的进步。其中&#xff0c;深度学习在生成模型中的应用尤为引人注目&#xff0c;它不仅为我们提供了生成全新、类似数据的能力&#xff0c;更为多个领域带来了革命性的变革。 …

经典的神经网络#1 Lenet

经典的神经网络#1 Lenet 关注B站查看更多手把手教学&#xff1a; 肆十二-的个人空间-肆十二-个人主页-哔哩哔哩视频 (bilibili.com) 网络结构介绍 LeNet的论文地址为&#xff1a;http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf。这篇论文名为《Gradient-Based Learnin…

Gitlab CICD 下载artifacts文件并用allure打开,或bat文件打开

allure命令行打开aritfacts报告 首先下载allure.zip&#xff0c;并解压 配置环境变量 使用命令行打开allure文件夹 allure open 2024-03-11-14-54-40 2024-03-11-14-54-40 包含index.html Bat文件打开artifacts There are 2 html reports in the download artifacts.zip S…

SpringMVC | SpringMVC中的 “数据绑定”

目录: “数据绑定” 介绍1.简单数据绑定 :绑定 “默认数据” 类型绑定 “简单数据类型” 类型 &#xff08;绑定Java“基本数据类型”&#xff09;绑定 “POJO类型”绑定 “包装 POJO”“自定义数据” 绑定 :Converter (自定义转换器) 作者简介 &#xff1a;一只大皮卡丘&#…

数组名结合指针的面试题的讲解

笔试题 第一题&#xff1a; 已知条件&#xff1a; 已知p为结构体指针变量&#xff0c;值为0x100000&#xff0c;并且结构体的大小为20字节&#xff0c;并且打印格式均为%p&#xff0c;%p不会在乎正负数&#xff0c;它会以补码的形式直接打印&#xff0c;0x1为16进制的1。 第一问…
最新文章