Example of a user-defined forward iterator?

Discussion in 'C++' started by Brett L. Moore, Jul 14, 2003.

  1. Hi,

    I have an octree class that I would like to (slowly) migrate to an
    STL-like container. I now need to implement two forward iterators for the
    class. I would like to use the iterators in the following manner:

    octree<GLfloat>::nodeIterator p;
    octree<GLfloat>::itemIterator q;

    for (p=foo.begin();p!=foo.end();p++)
    {
    // bleh
    }
    (or something reasonably similar). I have reviewed Stroustrup, but have
    found the limited examples difficult to adapt to my need. Can anyone
    provide an example of a simple container and its iterator?
    Thanks,Brett
     
    Brett L. Moore, Jul 14, 2003
    #1
    1. Advertising

  2. Brett L. Moore wrote:
    > Hi,
    >
    > I have an octree class that I would like to (slowly) migrate to an
    > STL-like container. I now need to implement two forward iterators for the
    > class. I would like to use the iterators in the following manner:
    >
    > octree<GLfloat>::nodeIterator p;
    > octree<GLfloat>::itemIterator q;
    >
    > for (p=foo.begin();p!=foo.end();p++)
    > {
    > // bleh
    > }
    > (or something reasonably similar). I have reviewed Stroustrup, but have
    > found the limited examples difficult to adapt to my need. Can anyone
    > provide an example of a simple container and its iterator?
    > Thanks,Brett


    Take a look at boost/iterator_adaptors.h. If you can't use it directly,
    the source code may give you a hint or two.

    Good luck,
    Buster
     
    Buster Copley, Jul 14, 2003
    #2
    1. Advertising

  3. "Thomas Matthews" <> wrote in message
    news:...
    > Brett L. Moore wrote:
    > > Hi,
    > >
    > > I have an octree class that I would like to (slowly) migrate to an
    > > STL-like container. I now need to implement two forward iterators for

    the
    > > class. I would like to use the iterators in the following manner:
    > >
    > > octree<GLfloat>::nodeIterator p;
    > > octree<GLfloat>::itemIterator q;
    > >
    > > for (p=foo.begin();p!=foo.end();p++)
    > > {
    > > // bleh
    > > }
    > > (or something reasonably similar). I have reviewed Stroustrup, but have
    > > found the limited examples difficult to adapt to my need. Can anyone
    > > provide an example of a simple container and its iterator?
    > > Thanks,Brett

    >
    > Many books on the subject say to inherit from std::forward_iterator.
    > See the <iterators> header.


    Maybe, but I don't believe there is such a class in the standard. I *think*
    it may be been in the original STL implementation but got dropped.

    Inheriting from std::iterator is a possibility but not required, I think.

    >
    > Also, read:
    > Effective C++
    > More Effective C++ {two separate books)
    > by Scott Meyers
    >
    > The Standard Template Libary
    > by Josuttis


    Especially the latter, I would say.

    john
     
    John Harrison, Jul 14, 2003
    #3
  4. Brett L. Moore

    Glen Low Guest

    > (or something reasonably similar). I have reviewed Stroustrup, but have
    > found the limited examples difficult to adapt to my need. Can anyone
    > provide an example of a simple container and its iterator?



    The clearest conceptual discussion of forward iterators and containers
    I've found was in Stepanov's (the creator of STL) old site:

    http://www.sgi.com/tech/stl/
    http://www.sgi.com/tech/stl/ForwardContainer.html
    http://www.sgi.com/tech/stl/ForwardIterator.html

    The definitions may be a little dated since STL got munged into the
    C++ Standard Library, but I found the site invaluable when I wanted to
    implement my own iterators. It has subtleties like what is
    "past-the-end" etc. which you wouldn't otherwise get from just reading
    code.
     
    Glen Low, Jul 15, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    614
    Jonathan Turkanis
    Feb 15, 2004
  2. Chris Curvey
    Replies:
    2
    Views:
    267
    Vinay Sajip
    Apr 13, 2006
  3. Oodini
    Replies:
    1
    Views:
    1,795
    Keith Thompson
    Sep 27, 2005
  4. vasili

    user-defined iterator

    vasili, Jun 15, 2007, in forum: C++
    Replies:
    3
    Views:
    1,220
    James Kanze
    Jun 16, 2007
  5. Replies:
    1
    Views:
    517
    Sion Arrowsmith
    Jul 10, 2008
Loading...

Share This Page