I have a class TContainer that is an aggregate of several stl collections pointers to TItems class.
I need to create an Iterator to traverse the elements in all the collections in my TContainer class abstracting the client of the inner workings.
What would be a good way to do this?. Should I crate a class that extends an iterator (if so, what iterator class should I extend), should I create an iterator class that is an aggregate of iterators?
I only need a FORWARD_ONLY iterator.
I.E, If this is my container:
typedef std::vector <TItem*> ItemVector;
class TContainer {
std::vector <ItemVector *> m_Items;
};
What would be a good Iterator to traverse all the items contained in the vectors of the m_Items member variable.
When I did my own iterator (a while ago now) I inherited from std::iterator and specified the type as the first template parameter. Hope that helps.
For forward iterators user forward_iterator_tag rather than input_iterator_tag in the following code.
This class was originally taken from istream_iterator class (and modified for my own use so it may not resemble the istram_iterator any more).
Check this documentation on iterator tags:
http://www.sgi.com/tech/stl/iterator_tags.html
Having just re-read the information on iterators:
http://www.sgi.com/tech/stl/iterator_traits.html
This is the old way of doing things (iterator_tags) the more modern approach is to set up iterator_traits<> for your iterator to make it fully compatible with the STL.