迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e687e1f1863550fc5964b1f55c0bbfe.png)
代码:
iterator.h:
#pragma once
#pragma warning(disable : 4996)
#include <iostream>
using namespace std;
class iter
{
public:
virtual bool hasNext() = 0;
virtual iter* next() = 0;
virtual void remove() = 0;
};
class dinerMenuIterator :public iter
{
public:
dinerMenuIterator(string ss)
{
s = ss;
i = 0;
}
virtual bool hasNext() {
if (s[i + 1]) return true;
else return false;
}
virtual dinerMenuIterator* next() {
i++;
return this;
}
virtual void remove() {
if (s[i] != NULL)
{
for (int j = i; j < s.size(); j++)
{
s[i] = s[i + 1];
}
}
s[s.size() - 1] = NULL;
}
string s;
int i;
};
class pancakeHouseMenuItera