473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Implementation of iterator in Composite Pattern

Hi there,

I am currently stuck on the following simple problem: implementing
an iterator for a class using the Composite Pattern. I feel like I
have to reimplement a complete new class for iterating over the
Element, since I cannot use directly the std::vector<>::iterator. Did
I miss anything, can I reuse something from the STL ?

Thanks
-Mathieu

class Element
{
public:
Element():key(-1) {}
virtual ~Element() {}
int getkey() const { return key; }
void setkey(int k) { key = k; }
private:
int key;
};

class CompositeElement : public Element
{
public:
CompositeElement():Elements() { }
typedef std::vector<Element*>::iterator iterator;
iterator begin() { return Elements.begin(); }
iterator end() { return Elements.end(); }
void push_back(Element * el)
{
Elements.push_back(el);
}

private:
std::vector<Element*Elements;
};

int main()
{
CompositeElement root;
// fill root ...

for (CompositeElement::iterator it = root.begin(); it != root.end();
it++)
{
Element* el = *it;
std::cout << el->getkey() << std::endl;
}

return 0;
}
Jan 18 '08 #1
1 3681
On Jan 18, 7:48 am, mathieu <mathieu.malate...@gmail.comwrote:
Hi there,

I am currently stuck on the following simple problem: implementing
an iterator for a class using the Composite Pattern. I feel like I
have to reimplement a complete new class for iterating over the
Element, since I cannot use directly the std::vector<>::iterator. Did
I miss anything, can I reuse something from the STL ?

Thanks
-Mathieu

class Element
{
public:
Element():key(-1) {}
virtual ~Element() {}
int getkey() const { return key; }
void setkey(int k) { key = k; }
private:
int key;

};

class CompositeElement : public Element
{
public:
CompositeElement():Elements() { }
typedef std::vector<Element*>::iterator iterator;
typedef typename std::vector<Element*>::iterator iterator;
iterator begin() { return Elements.begin(); }
iterator end() { return Elements.end(); }
void push_back(Element * el)
{
Elements.push_back(el);
}

private:
std::vector<Element*Elements;

};

int main()
{
CompositeElement root;
// fill root ...

for (CompositeElement::iterator it = root.begin(); it != root.end();
it++)
{
Element* el = *it;
std::cout << el->getkey() << std::endl;
}

return 0;

}
I'm not convinced that a CompositeElement is_an Element. Rather, i see
an abstract type from which both an Element and a Composite might be
classified by.

#include <iostream>
#include <vector>

struct Component
{
virtual ~Component() = 0;
};

Component::~Component() { }

template< typename K >
class Element : public Component
{
K key;
public:
Element() : key(-1) {}
Element(const K k) : key(k) {}
int getkey() const { return key; }
};

template< typename T >
class Composite : public Component
{
std::vector< T vt;
public:
Composite() : vt() { }
typedef typename std::vector< T >::iterator iterator;
iterator begin() { return vt.begin(); }
iterator end() { return vt.end(); }
void push_back(const T& p)
{
vt.push_back(p);
}
};

int main()
{
typedef Composite< Element<int>* CompositeElementInt;
CompositeElementInt root;

Element< int e;
root.push_back(&e);
Element< int e9(9);
root.push_back(&e9);

typedef CompositeElementInt::iterator CIter;
for ( CIter it = root.begin();
it != root.end();
++it )
{
std::cout << (*it)->getkey() << std::endl;
}
}

/*
-1
9
*/
Jan 18 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Smith | last post by:
Hi, Does anyone know a good (c++)example of the implementation of a "robust" list iterator (ie an iterator whereby insert/delete operations does not have any effect on the list)? I am looking...
2
by: Da Wang | last post by:
Hi,all, I am reading the Iterator part of Design Pattern. It defines a basic Iterator interface as shown below. My question is: what is the use of the protected constructor? Maybe that does not...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
7
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor...
1
by: alan.f22 | last post by:
HI First off, thanks for taking the time to read this. I have an class inheritance hierachy say: base class "E" and a bunch of derived classes (from "E") "A", "P", and "S" I have an STL...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template...
1
by: anto.anish | last post by:
Hi , Since, i did not want to write explicit instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of...
8
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
An example of a slightly more complicated class might be to have a collection of first names, and a collection of last names in your class. The IEnumerable functions then could return the complete...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.