--- At Mon, 3 Jun 2002 10:38:57 +0000, keithmac66 wrote:
Hi,
I'm struggling to understand how to use Iterator Adaptors, due to a lack of simple examples. This is disappointing, given that supposedly "Using iterator_adaptor, you can easily implement an iterator class" (www.boost.org/libs/utility/iterator_adaptors.htm). Can anyone help, by explaining how to implement a random access iterator for a custom collection class? e.g.:
template <class T> class DumbCollection { public: DumbCollection(T data[10]) { for (int i = 0; i < 10; i++) m_data[i] = data[i]; } iterator begin(); iterator end(); private: T m_data[10]; };
I assume that what you are looking for is the implementation of begin() and end(). The implementation is probably too long to include here. But I can give you some direction. iterator_adaptor is exactly that it adapts one iterator to another. To come from a container to an iterator you first need an iterator. However, boosts requirements on the base iterator are much less than a full- fledged iterator. Take a look at the documentation and implementation where it talks about default_iterator_policies. This shows you the functions that have to be implemented by your base iterator class. For random access iterators, you have to implement all of them. For simpler iterators you can implement less (there is another table describing the requirements). By implementing a base iterator then you should be able to follow the other recipies/examples for how to create the typedef and construct the iterators. Its all really based on your base iterator for your container. I have done several iterators over container like structures and this recipe works pretty well. There is a fair bit of code to get it all together. Hope this helped, ...Duane -- "If tyranny and oppression come to this land, it will be in the guise of fighting a foreign enemy." - James Madison