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DmkORwHj3R66_gBdENwdnVm40 * 2021 / JMB * */ #include #include class Iterator; class ConcreteIterator; class Aggregate { public: virtual ~Aggregate() {} virtual Iterator* createIterator() = 0; }; class ConcreteAggregate : public Aggregate { public: ConcreteAggregate(int N) { //do a memory allocation v.reserve(N); for (int i = 0; i < N; ++i) { //direct move in memory of i value v.emplace_back(i); } } ~ConcreteAggregate() {} Iterator* createIterator(); int getItem(int idx) { return v.at(idx); } unsigned int getSize() { return v.size(); } //emplace_back ==> move in memory void addItem(int obj) { v.emplace_back(obj); } private: std::vector v; }; class Iterator { public: virtual ~Iterator() {} virtual void first() = 0; virtual void next() = 0; virtual bool isDone() = 0; virtual int currentItem() = 0; }; class ConcreteIterator : public Iterator { public: ConcreteIterator(ConcreteAggregate *ag) : p_cag(ag), p_index(0) {} void first() { p_index = 0; } void next() { ++p_index; } bool isDone() { return p_index >= p_cag->getSize(); } int currentItem() { return p_cag->getItem(p_index); } private: ConcreteAggregate *p_cag; int p_index; }; Iterator *ConcreteAggregate::createIterator() { std::cout << "createIterator" << std::endl; return new ConcreteIterator(this); } int main() { ConcreteAggregate *concreteAggregate = new ConcreteAggregate(10); concreteAggregate->addItem(123); Iterator* it = concreteAggregate->createIterator(); for ( ; !it->isDone(); it->next()) { std::cout << "Item value: " << it->currentItem() << std::endl; } }