472,779 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 3652
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.