网站后台维护怎么做/石景山区百科seo
意图:提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示。
上下文:集合对象内部结构常常变化各异。对于这些集合对象,能否在不暴露其内部结构的同时,让外部Client透明地访问其中包含的元素,同时让这种“透明遍历”也为“同一种算法在多种集合对象上进行操作”提供可能?
UML
Iterator:定义访问和遍历元素的接口(.NET中定义了标准的IEnumrator接口)。ConcreteIterator:实现Iterator接口,同时在对Aggregate遍历时跟踪当前的位置。Aggregate:定义创建相应Iterator对象的接口(.NET中定义了标准的IEnumrable接口)。ConcreteAggregate:实现创建相应Iterator对象的接口,该操作返回一个适当的ConcreteIterator实例。
注意:.NET中的foreach关键字在编译时会自动创建迭代器对象,并使用该对象对集合进行遍历。.NET中的yield return关键字使得定义迭代器对象更加容易。
代码
#include <iostream>
#include <list>
#include <array>
#include <vector>
using namespace std;//Iterator迭代器抽象类
class Iterator
{
public:virtual void* First() = 0;virtual void* Next() = 0;virtual bool IsDone() = 0;virtual void* CurrentItem() = 0;
};//Aggregate聚集抽象类
class Aggregate
{
public:virtual Iterator *CreateIterator() = 0;
};//ConcreteAggregate具体聚集类 继承Aggregate
class ConcreteAggregate:public Aggregate
{
public:vector<void*> items;// Aggregate interface
public:Iterator *CreateIterator();int getCount() const;//既可以作为左值,也可以作为又值void** operator[](unsigned int index){if(items.size() <= index){items.resize(index+1);}return &items[index];}
};class ConcreteIterator:public Iterator
{
public:ConcreteAggregate *ca;int current;ConcreteIterator(ConcreteAggregate *c):ca(c){this->current = 0;}virtual void* First();virtual void* Next();virtual bool IsDone();virtual void* CurrentItem();
};Iterator *ConcreteAggregate::CreateIterator()
{return new ConcreteIterator(this);
}int ConcreteAggregate::getCount() const
{return items.size();
}void *ConcreteIterator::First()
{return *(*ca)[0];
}void *ConcreteIterator::Next()
{if(current < ca->getCount()){current++;}if(current < ca->getCount()){return *(*ca)[current];}return nullptr;
}bool ConcreteIterator::IsDone()
{return current >= ca->getCount()?true:false;
}void *ConcreteIterator::CurrentItem()
{return *(*ca)[current];
}class ConcreteIteratorDesc:public Iterator
{
public:ConcreteAggregate *ca;int current;ConcreteIteratorDesc(ConcreteAggregate *c):ca(c){this->current = c->getCount() - 1;}virtual void* First();virtual void* Next();virtual bool IsDone();virtual void* CurrentItem();
};
void *ConcreteIteratorDesc::First()
{if(ca->getCount() == 0){return nullptr;}return *(*ca)[ca->getCount() - 1];
}void *ConcreteIteratorDesc::Next()
{if(current >= 0){current--;}if(current >= 0){return *(*ca)[current];}return nullptr;
}bool ConcreteIteratorDesc::IsDone()
{return current < 0?true:false;
}
void *ConcreteIteratorDesc::CurrentItem()
{return *(*ca)[current];
}
int main()
{ConcreteAggregate ca;*(ca[0]) = (void*)5;*ca[1] = (void*)10;*ca[2] = (void*)15;*ca[3] = (void*)20;
// cout << (int) *ca[0] << endl;
// cout << (int) *ca[1] << endl;
// cout << ca.getCount() << endl;Iterator *i = new ConcreteIterator(&ca);cout << "开始遍历" << endl;while(!i->IsDone()){cout << (int)i->CurrentItem() << endl;i->Next();}Iterator *i_desc = new ConcreteIteratorDesc(&ca);cout << "开始反向遍历" << endl;while(!i_desc->IsDone()){cout << (int)i_desc->CurrentItem() << endl;i_desc->Next();}cout << "--end--" << endl;return 0;
}
结果:
开始遍历
5
10
15
20
开始反向遍历
20
15
10
5
--end--
相关文章:

**20.迭代器模式(Iterator)
意图:提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示。 上下文:集合对象内部结构常常变化各异。对于这些集合对象,能否在不暴露其内部结构的同时,让外部Client透明地访问其中包含的元素…...

计算机视觉与深度学习 | 视觉里程计理论
===================================================== github:https://github.com/MichaelBeechan CSDN:https://blog.csdn.net/u011344545 ===================================================== 视觉里程计理论基础 1 、立体视觉中的三维测量及几何约束2 、立体视觉匹…...

Go网络请求中配置代理
如何配置代理 不配置代理,本地请求google等会超时 package mainimport ( "fmt" "net/http" "time")func main() { // 创建一个自定义的 Transport 实例 //transport : &http.Transport{ // Proxy: func(req *http.Request) (*url…...

【ArcGIS】基本概念-矢量空间分析
栅格数据与矢量数据 1.1 栅格数据 栅格图是一个规则的阵列,包含着一定数量的像元或者栅格 常用的栅格图格式有:tif,png,jpeg/jpg等 1.2 矢量数据 矢量图是由一组描述点、线、面,以及它们的色彩、位置的数据&#x…...

OSCP系列靶场-Esay-Moneybox保姆级
OSCP系列靶场-Esay-Moneybox 目录 OSCP系列靶场-Esay-Moneybox总结准备工作信息收集-端口扫描目标开放端口收集目标端口对应服务探测 信息收集-端口测试21-FTP端口的信息收集21-FTP版本版本信息21-FTP端口匿名登录测试(存在)21-FTP端口-文件GET收集21-FTP端口-PUT上传测试 22-S…...

[python 刷题] 128 Longest Consecutive Sequence
[python 刷题] 128 Longest Consecutive Sequence 题目: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time. 这题给了一个没有排序的数组&#x…...

SpringMVC之JSON数据返回与异常处理机制
目录 一.SpringMVC的JSON数据返回 1.导入Maven依赖 2.配置spring-mvc.xml 3.ResponseBody注解的使用 3.1案例演示 1.List集合转JSON 2.Map集合转JSON 3.返回指定格式String 4. ResponseBody用法 5.Jackson 5.1介绍 5.2常用注解 二.异常处理机制 1.为什么要全局异常处…...

【第四阶段】kotlin语言的定义类和field关键字学习
1.普通成员变量背后隐士代码 为什么在kotlin中是private 可以直接调用,隐式代码如下 package Kotlin.Stage4class Test54{var name"kotlin"/*背后做的事NotNullprivate String name"kotlin";public void setName(NotNull String name){this.na…...

OpenResty使用漏桶算法实现限流
前言 其它项目组需要调用接口,添加接口限流,防止项目被狂掉宕机。生产用了openresty,所以在openresty上添加按接口限流,同时,需按照不同接口有不同的限流规则,使用openresty中内置的漏桶算法方式限流。 漏…...

Activiti源码跟踪之模型Model操作
Activiti源码跟踪之模型Model操作 模型model设计到的表ACT_RE_MODEL、ACT_GE_BYTEARRAY ACT_RE_MODEL表结构: CREATE TABLE ACT_RE_MODEL (ID_ varchar(64) COLLATE utf8_bin NOT NULL,REV_ int(11) DEFAULT NULL,NAME_ varchar(255) COLLATE utf8_bin DEFAULT N…...

C#-WinForm-发送邮件
登录QQ邮箱——设置——开启“POP3/SMTP服务” 登陆QQ邮箱→打开设置→开启“POP3/SMTP服务”,获取“授权码” 简单总结一下: 1、使用SmtpClient发送电子邮件是很简单的,只要正确创建了MailMessage对象和SmtpClient就可以很容易的发送出去电…...

Springboot整合jdbc和Mybatis
目录 整合jdbc 1. 新建项目 2. 编写yaml配置文件连接数据库 3. 测试类 使用原生的jdbcTemplate进行访问测试 使用Druid连接池 1. 添加类型 2. 初始化连接池 3. 编写config类 配置Druid数据源监视 整合Mybatis 1. 导入依赖 2. 编写mapper接口 3. 编写实体类 4. 编…...

日常生活中的常用命令及操作
目录 一、Windows11 中查看网卡名称 及ip地址 二、查看硬件的详细信息 三、查看显卡声卡详细信息及厂商 四、C盘清理 第一步 输入wini 开启Windows设置主界面 第二步 存储中还有一个叫存储感知的功能 第三步 更改新内容的保存位置 第四步 怕误C盘内的东西可以 查看详细的…...

【C++杂货铺】国庆中秋特辑——多态由浅入深详细总结
文章目录 一、多态的概念二、多态的定义及实现2.1 多态的构成条件2.2 虚函数2.3 虚函数的重写2.4 虚函数重写的两个例外2.4.1 协变(基类与派生类虚函数返回值类型不同)2.4.2 析构函数的重写(基类与派生类析构函数的名字不同) 2.5 …...

MongoDB基础详解
一、MongoDB概述 MongoDB 是一个基于 分布式文件存储 的开源 NoSQL 数据库系统,由 C 编写的。MongoDB 提供了 面向文档 的存储方式,操作起来比较简单和容易,支持“无模式”的数据建模,可以存储比较复杂的数据类型,是一…...

解锁前端Vue3宝藏级资料 第五章 Vue 组件应用 4 ( provide 和 inject )
5.5 provide 和 inject 前面的知识告诉我们vue中组件之间传递值需要使用props来完成,但是props也有一定局限性。这个时候在vue3中还有另外的解决方法。那就是使用 provide 和 inject 允许父组件将数据传递给所有后代组件,而不管组件层次结构有多深。你要…...

【List篇】LinkedList 详解
目录 成员变量属性构造方法add(), 插入节点方法remove(), 删除元素方法set(), 修改节点元素方法get(), 取元素方法ArrayList 与 LinkedList的区别Java中的LinkedList是一种实现了List接口的 双向链表数据结构。链表是由一系列 节点(Node)组成的,每个节点包含了指向 上一个…...

推动统一供应链“度量衡”,上汽大通突破传统拥抱SaaS生态
中国汽车市场规模已连续14年位居世界第一,目前占世界汽车份额31%。近年来,物联网、人工智能、电池等技术的快速发展,也为中国从汽车大国逐步迈向汽车强国注入巨大动力。在新一轮的汽车产业变革中,构建一个更智能、更高效协同的供应…...

蓝牙核心规范(V5.4)10.9-BLE 入门笔记之GAP
1.概述 蓝牙核心规范的通用访问配置文件(GAP)部分定义了与设备发现和在两个设备之间建立连接有关的过程。如何执行数据的基本无连接通信、如何使用周期性广播(参见 PADVB-LE Periodic Advertising Broadcast)以及如何设置等时通信(参见 LE BIS和LE CIS - Isochronous Com…...

nginx 配置 ssl
1.1 Nginx如果未开启SSL模块,配置Https时提示错误 原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经安装过了,怎么添加模块࿰…...

家居设计软件Live Home 3D Pro mac中文版特点介绍
Live Home 3D Pro mac是一款专业的3D家居设计软件,可以帮助用户轻松创建和设计家居平面图和3D模型,并进行渲染和虚拟漫游。 Live Home 3D Pro mac软件特点 1. 界面友好:Live Home 3D Pro的界面友好,操作简单方便…...

OkHttp - 现代应用网络的方式
官网:Overview - OkHttp HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth. OkHttp is an HTTP client that’s efficient by default: HTTP/2 s…...

SpringBoot3基础:最简项目示例
说明 本文建立一个最基本的SpringBoot3项目,依赖项仅包含 spring-web(SpringMVC)。 备注:SpringBoot3需要JDK17支持,配置方法参考: SpringBoot3项目中配置JDK17 项目结构图示 POM <?xml version&qu…...

flex:1详解,以及flex:1和flex:auto的区别
什么是flex:1? 在css中,我们经常可以看到这样的写法: .box {display: flex; }.item {flex: 1; }这里的flex:1相当于flex: 1 1 0%,它是一个简写属性,表示项目(flex item)在弹性容器…...

在VMware虚拟机中固定CentOS系统ip(使用桥接模式)
目录 一、前置说明二、前置准备2.1、切换虚拟机网络为桥接模式2.2、查看本机网络信息 三、配置CentOS系统IP3.1、进入系统输入ip addr 查看本机网络配置名称3.2、查看网络配置目录,网络配置文件名称3.3、修改网络配置文件 ifcfg-ens33 固定IP3.4、重启网络 一、前置…...

怎样才能让百度搜索到自己的博客?--九五小庞
怎么把自己的博客推荐到百度、Google等主要搜索引擎? 如果不把你的博客提交到各大搜索引擎中,它们一般是不会收录你的博客的,你可以先尝试一下看看能不能在百度搜到你的博客吧。 如果搜不到的话说明你的博客还没有被百度收录,那么…...

【学习笔记】多模态综述
多模态综述 前言1. CLIP & ViLT2. ALBEF3. VLMO4. BLIP5. CoCa6. BeiTv3总结参考链接 前言 本篇学习笔记虽然是多模态综述,本质上是对ViLT后多模态模型的总结,时间线为2021年至2022年,在这两年,多模态领域的模型也是卷的飞起…...

MLAgents (0) Unity 安装及运行
1、下载ML-Agents 下载地址 GitHub - Unity-Technologies/ml-agents: The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinfo…...

typename关键字详解(消除歧义)
typename关键字详解 文章目录 typename关键字详解定义用法1.和class同义,用于引入泛型编程中所用到的模板参数2.用来消除歧义,告诉编译器后面的是类型名而不是变量名 定义 typename相当于泛型编程中class的同义关键字,用来指出模板类型所依赖…...

设计模式_解释器模式
解释器模式 案例 角色 1 解释器基类 (BaseInterpreter) 2 具体解释器1 2 3... (Interperter 1 2 3 ) 3 内容 (Context) 4 用户 (user) 流程 (上下文) ---- 传…...