当前位置:网站首页>光标——迭代器模式
光标——迭代器模式
2022-04-22 04:08:00 【闲云野雀】
迭代器模式

迭代器有时又称光标,可在容器对象(container,例如链表或数组)上遍访的接口,设计人员无需关心容器对象的内存分配的实现细节。
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <Windows.h>
using namespace std;
/*以下只是学习迭代器的思想,仿制的伪迭代器,与STL迭代器并不相关*/
//抽象迭代器
class disable_iterator
{
public:
//获得容器中第一个元素
virtual int Current() = 0;
//获得容器中下一个元素
virtual void Next() = 0;
//容器是否为空
virtual bool isEmpty() = 0;
};
//抽象容器
class Container
{
public:
vector<int> array;
public:
virtual disable_iterator* createIterator() = 0;
};
//具体迭代器
class real_disable_iterator : public disable_iterator
{
private:
Container* contain;
int count;
public:
real_disable_iterator(Container* con):contain(con),count(0){
}
//获得容器中第一个元素
virtual int Current()
{
return contain->array[count];
}
//获得容器中下一个元素
virtual void Next()
{
count++;
return;
}
//容器是否为空
virtual bool isEmpty()
{
return count == contain->array.size();
}
};
//具体容器
class myContainer : public Container
{
public:
void push_back(int n)
{
array.push_back(n);
}
virtual disable_iterator* createIterator()
{
return new real_disable_iterator(this);
}
};
int main()
{
myContainer con;
con.push_back(1);
con.push_back(5);
con.push_back(9);
disable_iterator* myIter = con.createIterator();
while(!myIter->isEmpty())
{
cout << myIter->Current() << endl;
myIter->Next();
}
system("pause");
return 1;
}
我们上述的迭代器和STL迭代器相差甚远,但是迭代器的主要思想是相同的,都是将被迭代的对象交给我们的迭代器进行管理,客户端只需通过迭代器接口就可以进行操作,无需了解被迭代对象的内部数据结构,迭代器相当于一层防护衣,对被管理的对象进行了封装。
版权声明
本文为[闲云野雀]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_34850023/article/details/124306834
边栏推荐
- Take a look at this guide when the Hackathon competition is going on
- 专家有料 | 张祖优:腾讯云DevSecOps实践与开源治理探索
- 【树莓派C语言开发】实验12:PCF8591模数转换器模块
- Record the solution to the failure of configuring MySQL remote connection for ECS once
- Installation team and installation free version
- 调用函数时,关于传参那些事~
- . net 20th anniversary learning challenge Net mobile application
- 搜索内容入库
- 数据挖掘系列(2)_Excel的数据挖掘插件连接SQL Server
- MongoDB——聚合管道之$match操作
猜你喜欢

Take a look at this guide when the Hackathon competition is going on

rsync远程同步

Open source culture is still shining - in the openeuler community, with technology and idea, you are the protagonist

sumo 教程——高速公路

【树莓派C语言开发】实验12:PCF8591模数转换器模块

tensorflow报错:returned a result with an error set解决方案

Registration process of New Zealand company and materials required

L'échange et le partage des données sont au cœur de l'amélioration de l'efficacité de l'utilisation des données dans l'industrie des transports.

LeetCode 63. 不同路径 II

export ‘createStore‘ (imported as ‘createStore‘) was not found in ‘./store/index.js‘ (possible expor
随机推荐
Senior vice president of Apple hardware technology revealed that self-developed M1 is too difficult
Autodesk Genuine Service2020删除
【网络实验】/主机/路由器/交换机/网关/路由协议/RIP+OSPF/DHCP
Sub database and sub table
Homogeneous nucleation of ice by lammps
小程序 关于分包
Data cleaning chapter05 | data grouping and data imbalance
rsync远程同步
网页性能优化
24 pictures to conquer border image
記一次雲服務器配置mysql 遠程連接失敗的解决方案
How to solve the problem of DataGrid flash back? MySQL
Data mining series (2)_ The data mining plug-in of Excel connects to SQL server
LeetCode 63. 不同路径 II
How to generate PCB real-time snapshot in 3D in Ad
RF and microwave design software
Sumo tutorial - Highway
Installation team and installation free version
DO447Ansible Tower导航
Ivorysql 1.2 has come