7

I have been searching for sample code creating iterator for my own container, but I haven't really found a good example. I know this been asked before (Creating my own Iterators) but didn't see any satisfactory answer with examples.

I am looking for simple sample code to start how to design my own iterator.

Thanks

Community
  • 1
  • 1
Kode
  • 71
  • 1
  • 2

4 Answers4

6

Here you could find good intro for creating custom iterators. Also take a look on the Boost.Iterator Library.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
2

I found Matthew Wilson's 'extended STL' very educative on the subject. Contains lots of do's and don'ts, plus tons of practical programming tips. I think this guy really knows what he's doing. (created libraries for that, too)

xtofl
  • 40,723
  • 12
  • 105
  • 192
0

Nicolai Josuttis has an example of a user defined iterator in his book: C++ Standard Library, a tutorial and a reference.

Here is the example online:

http://www.josuttis.com/libbook/iter/assoiter.hpp http://www.josuttis.com/libbook/iter/assoiter.cpp

navigator
  • 1,588
  • 1
  • 13
  • 19
0

Take a look on this article which describes how to implement a custom virtual iterator for your classes: article

It has one significant advantage - you can create an abstract base iterator class and inherit it with a few custom iterators for your own containers and maybe for some STL containers. So you will be able to use iterators dynamically - your functions will use a pointer to the abstract iterator class while other code will be able to choose what containers should be used.

Roman Kruglov
  • 3,375
  • 2
  • 40
  • 46