【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;
};
相关推荐
Chester_19992 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲3 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员3 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导3 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan3 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
小灰灰搞电子4 小时前
完全驾驭 Qt 与数据库:C++ ORM 框架 QxOrm 原理与实践指南
数据库·c++·qt
QT界面美化性能优化4 小时前
QT+AI:使用AI技术为QT应用程序赋能
c++·人工智能·qt·opencv·qt教程·qt6.3
m0_695251494 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
大衛說5 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
学习星球6 小时前
单调栈——从“找下一个更大的“到柱状图中的最大矩形
数据库·c++·算法·leetcode·xcode