Reputation: 71
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
Upvotes: 7
Views: 3432
Reputation: 3547
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.
Upvotes: 0
Reputation: 1596
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
Upvotes: 0
Reputation: 41519
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)
Upvotes: 2
Reputation: 99605
Here you could find good intro for creating custom iterators. Also take a look on the Boost.Iterator Library.
Upvotes: 6