求 有关暗恋的耽美小说说重新暗恋【重生】 梅蕴刀百度云

Iterator模式(C++迭代器模式)
基本上来说,Iterator模式并没有什么可多说得,在STL中见得实在太多了,一般用于遍历数据结构,其实现也相对简单。
//////////////////////////////////////////////////////////////////////////
// author: Jeson Yang
//file:main.cpp
//////////////////////////////////////////////////////////////////////////
class Aggregate
virtual ~Aggregate(){}
virtual Interator* CreateItrator()=0{};
virtual int GetSize()=0{}
virtual int GetItem(int index)=0{}
protected:
Aggregate(){}
class ConcreteAggregate:public Aggregate
enum{size=3};
ConcreteAggregate()
for(int i=0;i GetSize()) m_index++;
bool IsDone(){return(m_index==m_ag->GetSize());}
int CurrentItem(){return m_ag->GetItem(m_index);}
Aggregate* m_
Interator* ConcreteAggregate::CreateItrator()
return new ConcreteInterator(this);
void main()
Aggregate* ag=new ConcreteAggregate();
Interator* it=new ConcreteInterator(ag);
for(;!(it->IsDone());it->Next())
cout<CurrentItem()<<c++设计模式(22)
& & & & 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
& &适用性:
& & & & 访问一个聚合对象的内容而无需暴露它的内部表示。
& & & & 支持对聚合对象的多种遍历。
& & & & 为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。
& & & & & & & & & & & & & & & & &&
#include &iostream&
#include &string&
#include &vector&
class Iterator
Iterator(){}
virtual ~Iterator(){};
virtual string First() = 0;
virtual string Next() = 0;
virtual string GetCur() = 0;
virtual bool IsEnd() = 0;
class Aggregate
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
ConcreteIterator(Aggregate *p_aggregate):m_aggregate(p_aggregate),m_cnt(0){}
string First()
return m_aggregate-&POp(0);
string Next()
if (m_cnt & m_aggregate-&Count())
str = m_aggregate-&POp(m_cnt);
string GetCur()
return m_aggregate-&POp(m_cnt);
bool IsEnd()
return m_cnt &= m_aggregate-&Count() ? true :
~ConcreteIterator(){ }
Aggregate *m_
class ConcreteAggregate : public Aggregate
ConcreteAggregate() :m_pIterator(NULL)
m_vecItem.clear();
~ConcreteAggregate()
if (NULL != m_pIterator)
delete m_pI
m_pIterator = NULL;
int Count()
return m_vecItem.size();
void Push(const string &strValue)
m_vecItem.push_back(strValue);
string POp(const int cnt)
if (cnt & Count())
str = m_vecItem[cnt];
Iterator* CreateIterator()
if (NULL == m_pIterator)
m_pIterator = new ConcreteIterator(this);
return m_pI
vector&string& m_vecI
Iterator *m_pI
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& &&
iter-&Next();
system(&pause&);
改进一下也许更好懂一点:
#include &iostream&
#include &string&
#include &vector&
class Iterator
Iterator(){}
virtual ~Iterator(){};
virtual string First() = 0;
virtual string Next() = 0;
virtual string GetCur() = 0;
virtual bool IsEnd() = 0;
class Aggregate
virtual int Count() = 0;
virtual void Push(const string &strValue) = 0;
virtual string POp(const int cnt) = 0;
class ConcreteIterator : public Iterator
ConcreteIterator(Aggregate *p_aggregate):m_aggregate(p_aggregate),m_cnt(0){}
string First()
return m_aggregate-&POp(0);
string Next()
if (m_cnt & m_aggregate-&Count())
str = m_aggregate-&POp(m_cnt);
string GetCur()
return m_aggregate-&POp(m_cnt);
bool IsEnd()
return m_cnt &= m_aggregate-&Count() ? true :
~ConcreteIterator(){ }
Aggregate *m_
class ConcreteAggregate : public Aggregate
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)
if (cnt & Count())
str = m_vecItem[cnt];
vector&string& m_vecI
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& &&
iter-&Next();
iter = NULL;
system(&pause&);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:163705次
积分:4156
积分:4156
排名:第6352名
原创:252篇
转载:12篇
评论:49条
文章:14篇
阅读:7282
(3)(5)(1)(2)(4)(3)(9)(1)(1)(2)(26)(5)(11)(8)(27)(2)(5)(3)(8)(7)(6)(10)(24)(18)(19)(1)(1)(7)(3)(6)(10)(10)(15)Copyright & HAOSOLA
Powered by:
模板提供:

我要回帖

更多关于 重新暗恋by梅蕴刀 的文章

 

随机推荐