迭代器:
    意图:
        提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
    适用性:
        访问一个聚合对象的内容而无需暴露它的内部表示。

        支持对聚合对象的多种遍历。

        为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。

                                  

c++设计模式----迭代器模式iterator_ios

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Iterator
{
public:
Iterator(){}
virtual ~Iterator(){};
virtual string First() = 0;
virtual string Next() = 0;
virtual string GetCur() = 0;
virtual bool IsEnd() = 0;
};

class Aggregate
{
public:
virtual int Count() = 0;
virtual void Push(const string &strValue) = 0;
virtual string POp(const int cnt) = 0;
virtual Iterator* CreateIterator() = 0;
};

class ConcreteIterator : public Iterator
{
public:
ConcreteIterator(Aggregate *p_aggregate):m_aggregate(p_aggregate),m_cnt(0){}

string First()
{
return m_aggregate->POp(0);
}

string Next()
{
string str;
m_cnt++;
if (m_cnt < m_aggregate->Count())
{
str = m_aggregate->POp(m_cnt);
}

return str;
}

string GetCur()
{
return m_aggregate->POp(m_cnt);
}

bool IsEnd()
{
return m_cnt >= m_aggregate->Count() ? true : false;
}

~ConcreteIterator(){ }

private:
Aggregate *m_aggregate;
int m_cnt;
};

class ConcreteAggregate : public Aggregate
{
public:
ConcreteAggregate() :m_pIterator(NULL)
{
m_vecItem.clear();
}

~ConcreteAggregate()
{
if (NULL != m_pIterator)
{
delete m_pIterator;
m_pIterator = NULL;
}
}

int Count()
{
return m_vecItem.size();
}

void Push(const string &strValue)
{
m_vecItem.push_back(strValue);
}

string POp(const int cnt)
{
string str;
if (cnt < Count())
{
str = m_vecItem[cnt];
}
return str;
}

Iterator* CreateIterator()
{
if (NULL == m_pIterator)
{
m_pIterator = new ConcreteIterator(this);
}

return m_pIterator;
}

private:
vector<string> m_vecItem;
Iterator *m_pIterator;
};

int main()
{
ConcreteAggregate* pName = NULL;
pName = new ConcreteAggregate();
if (NULL != pName)
{
pName->Push("hello");
pName->Push("word");
pName->Push("cxue");
}
Iterator* iter = NULL;
iter = pName->CreateIterator();
if (NULL != iter)
{
string strItem = iter->First();
while (!iter->IsEnd())
{
cout << iter->GetCur() << " is ok" << endl;
iter->Next();
}
}
system("pause");

return 0;
}

改进一下也许更好懂一点:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Iterator
{
public:
Iterator(){}
virtual ~Iterator(){};
virtual string First() = 0;
virtual string Next() = 0;
virtual string GetCur() = 0;
virtual bool IsEnd() = 0;
};

class Aggregate
{
public:
virtual int Count() = 0;
virtual void Push(const string &strValue) = 0;
virtual string POp(const int cnt) = 0;
};

class ConcreteIterator : public Iterator
{
public:
ConcreteIterator(Aggregate *p_aggregate):m_aggregate(p_aggregate),m_cnt(0){}

string First()
{
return m_aggregate->POp(0);
}

string Next()
{
string str;
m_cnt++;
if (m_cnt < m_aggregate->Count())
{
str = m_aggregate->POp(m_cnt);
}

return str;
}

string GetCur()
{
return m_aggregate->POp(m_cnt);
}

bool IsEnd()
{
return m_cnt >= m_aggregate->Count() ? true : false;
}

~ConcreteIterator(){ }

private:
Aggregate *m_aggregate;
int m_cnt;
};

class ConcreteAggregate : public Aggregate
{
public:
ConcreteAggregate()
{
m_vecItem.clear();
}

~ConcreteAggregate()
{
}

int Count()
{
return m_vecItem.size();
}

void Push(const string &strValue)
{
m_vecItem.push_back(strValue);
}

string POp(const int cnt)
{
string str;
if (cnt < Count())
{
str = m_vecItem[cnt];
}
return str;
}

private:
vector<string> m_vecItem;
};

int main()
{
ConcreteAggregate* pName = NULL;
pName = new ConcreteAggregate();
if (NULL != pName)
{
pName->Push("hello");
pName->Push("word");
pName->Push("cxue");
}

Iterator* iter = new ConcreteIterator(pName);

if (NULL != iter)
{
string strItem = iter->First();
while (!iter->IsEnd())
{
cout << iter->GetCur() << " is ok" << endl;
iter->Next();
}
}

delete iter;
iter = NULL;

system("pause");

return 0;
}