Correct way to define iterators

  • Follow


Hi I was wondering about the correct way to define new iterator types. With the
old SGI STL you could do something like:

#include <iterator>

class my_iter : public random_access_iterator<double> {
        //...
};

to instantly give my_iter all the correct tags and traits of a random access
iterator. Howerer as I understand the iterator classes are not in the C++ std
so this would be the wrong way. What is the best way to solve this is it to
typedef all iterator traits such as random_access_iterator_tag, difference_type
etc for every iterator I write like:

class my_iter {
        typdef random_access_iterator_tag iterator_category;
        typedef double value_type;
        //...
};

or is there a better way?

/Daniel Aarno

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Daniel 8/5/2005 3:15:56 PM

Daniel Aarno wrote:
> Hi I was wondering about the correct way to define new iterator types.
[snip]
> or is there a better way?

Yes, a much better way. See
http://www.boost.org/libs/iterator/doc/index.html
(it will hopefully be part of the next C++ standard).

Tom


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Tom 8/5/2005 5:47:03 PM


"Tom Widmer" <tom_usenet@hotmail.com> writes:

> Daniel Aarno wrote:
>> Hi I was wondering about the correct way to define new iterator types.
> [snip]
>> or is there a better way?
>
> Yes, a much better way. See
> http://www.boost.org/libs/iterator/doc/index.html
> (it will hopefully be part of the next C++ standard).

Hoping isn't going to make it happen.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply David 8/6/2005 9:59:52 AM

Daniel Aarno <macbishop@users.sourceforge.net> writes:

> Hi I was wondering about the correct way to define new iterator types. With the
> old SGI STL you could do something like:
>
> #include <iterator>
>
> class my_iter : public random_access_iterator<double> {
>         //...
> };
>
> to instantly give my_iter all the correct tags and traits of a random access
> iterator. Howerer as I understand the iterator classes are not in the C++ std
> so this would be the wrong way. What is the best way to solve this is it to
> typedef all iterator traits such as random_access_iterator_tag, difference_type
> etc for every iterator I write like:
>
> class my_iter {
>         typdef random_access_iterator_tag iterator_category;
>         typedef double value_type;
>         //...
> };
>
> or is there a better way?

The best way currently known, if I do say so myself, is
http://www.boost.org/libs/iterator/doc/iterator_facade.html.  Start
with the abstract and then move on to the tutorial.

HTH,
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply David 8/6/2005 10:00:34 AM

David Abrahams wrote:
> "Tom Widmer" <tom_usenet@hotmail.com> writes:
>
> > Daniel Aarno wrote:
> >> Hi I was wondering about the correct way to define new iterator types.
> > [snip]
> >> or is there a better way?
> >
> > Yes, a much better way. See
> > http://www.boost.org/libs/iterator/doc/index.html
> > (it will hopefully be part of the next C++ standard).
>
> Hoping isn't going to make it happen.

Wasn't it supposed to be in TR1? What happened? 

Tom


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Tom 8/8/2005 3:04:53 PM

Tom Widmer wrote:
> 
> Wasn't it supposed to be in TR1? What happened? 
> 

The iterator adaptors proposal depended somewhat on the proposed new 
iterator traits. That latter proposal had some problems, and rather than 
delay TR1 we removed it, along with the adaptors. There's work currently 
going on to resolve the problems with iterator traits, and I assume once 
that's been sorted out we'll move ahead with the iterator adaptors.

-- 

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Pete 8/8/2005 4:40:59 PM

On 5 Aug 2005 13:47:03 -0400, "Tom Widmer" <tom_usenet@hotmail.com>
wrote:
>
>Daniel Aarno wrote:
>> Hi I was wondering about the correct way to define new iterator types.
>[snip]
>> or is there a better way?
>
>Yes, a much better way. See
>http://www.boost.org/libs/iterator/doc/index.html

Before you drown in Boost code you may take a look at e.g.
http://msdn.microsoft.com/msdnmag/issues/01/04/STL/default.aspx
http://www.langer.camelot.de/Articles/C++Report/OutputIterators/OutputIterators.html

Good luck!

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply rpbg123 8/8/2005 7:53:26 PM

Daniel Aarno wrote:
> #include <iterator>
> 
> class my_iter : public random_access_iterator<double> {
>         //...
> };
> 
> to instantly give my_iter all the correct tags and traits of a random access
> iterator. Howerer as I understand the iterator classes are not in the C++ std
> so this would be the wrong way. What is the best way to solve this

Just change your code into something like

   class my_iter : public iterator<random_access_iterator_tag, double>
   {
    ...
   };

iterator<> has default typedefs for the other three template parameters,
so the above version had already everything you need.

Ali

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply albrecht 8/9/2005 7:20:38 PM

7 Replies
308 Views

(page loaded in 0.116 seconds)

Similiar Articles:













7/28/2012 6:58:47 AM


Reply: