同步操作将从 hqgboy/design-patterns-cpp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/** C++ Design Patterns: Iterator* Author: Jakub Vojvoda [github.com/JakubVojvoda]* 2016** Source code is licensed under MIT License* (for more details see LICENSE)**/#include <iostream>#include <stdexcept>#include <vector>class Iterator;class ConcreteAggregate;/** Aggregate* defines an interface for aggregates and it decouples your* client from the implementation of your collection of objects*/class Aggregate{public:virtual ~Aggregate() {}virtual Iterator *createIterator() = 0;// ...};/** Concrete Aggregate* has a collection of objects and implements the method* that returns an Iterator for its collection**/class ConcreteAggregate : public Aggregate{public:ConcreteAggregate( const unsigned int size ){list = new int[size]();count = size;}~ConcreteAggregate(){delete[] list;}Iterator *createIterator();unsigned int size() const{return count;}int at( unsigned int index ){return list[ index ];}// ...private:int *list;unsigned int count;// ...};/** Iterator* provides the interface that all iterators must implement and* a set of methods for traversing over elements*/class Iterator{public:virtual ~Iterator() { /* ... */ }virtual void first() = 0;virtual void next() = 0;virtual bool isDone() const = 0;virtual int currentItem() const = 0;// ...};/** Concrete Iterator* implements the interface and is responsible for managing* the current position of the iterator*/class ConcreteIterator : public Iterator{public:ConcreteIterator( ConcreteAggregate *l ) :list( l ), index( 0 ) {}~ConcreteIterator() {}void first(){index = 0;}void next(){index++;}bool isDone() const{return ( index >= list->size() );}int currentItem() const{if ( isDone() ){return -1;}return list->at(index);}// ...private:ConcreteAggregate *list;unsigned int index;// ...};Iterator *ConcreteAggregate::createIterator(){return new ConcreteIterator( this );}int main(){unsigned int size = 5;ConcreteAggregate list = ConcreteAggregate( size );Iterator *it = list.createIterator();for ( ; !it->isDone(); it->next()){std::cout << "Item value: " << it->currentItem() << std::endl;}delete it;return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。