Example of a user-defined forward iterator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett L. Moore

    Example of a user-defined forward iterator?

    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
  • Buster Copley

    #2
    Re: Example of a user-defined forward iterator?

    Brett L. Moore wrote:[color=blue]
    > 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[/color]

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

    Good luck,
    Buster

    Comment

    • John Harrison

      #3
      Re: Example of a user-defined forward iterator?


      "Thomas Matthews" <tomatthews@sbc global.net> wrote in message
      news:3F12919A.6 090201@sbcgloba l.net...[color=blue]
      > Brett L. Moore wrote:[color=green]
      > > 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[/color][/color]
      the[color=blue][color=green]
      > > 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[/color]
      >
      > Many books on the subject say to inherit from std::forward_it erator.
      > See the <iterators> header.[/color]

      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.
      [color=blue]
      >
      > Also, read:
      > Effective C++
      > More Effective C++ {two separate books)
      > by Scott Meyers
      >
      > The Standard Template Libary
      > by Josuttis[/color]

      Especially the latter, I would say.

      john


      Comment

      • Glen Low

        #4
        Re: Example of a user-defined forward iterator?

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


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





        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.

        Comment

        Working...