Boost logo

Boost Users :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-06-03 13:09:07


"Duane Murphy" <duanemurphy_at_[hidden]> wrote in message
news:20020603150922.32043_at_mail.murphyslogic.com...
> --- 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
> >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];
> > };

For this one, plain T* makes a fine iterator type, assuming you intend for
it to iterate through the elements of m_data.

However, if you want to do it with iterator_adaptor so that you don't have
pointers hanging around, the answer is trivial:

    typedef boost::iterator_adaptor<T*, boost::default_iterator_policies>
iterator;

> 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.

In fact, the Base type need not be an iterator at all. For example, the
counting iterator adaptor can accept int as the Base type.

> 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.

This statement is a bit misleading. It shows the functions that your
Policies class must implement.

-Dave


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net