creating iterators

Started by
4 comments, last by Andrew Russell 18 years, 11 months ago
Hey, I am looking for some documention on creating a iterator. it is for a list like class i am creating, so anyone knows of any documention for creating a list or list like iterator, i would like to know where, thanks.
Advertisement
Well, if you want a list style iterator, it's known as a bidirectional iterator. As for implementing it, depends entirely on how you implemented your data-structure.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

well this is basically a custom list class i am creating, and this is the next this i am trying to implement. my class has the stuff as the stl list.
A list iterator is really just a container of a pointer to the item in the list. Im assuming the items in your list have a next and/or previous pointer to itself, something like:

template< class T >struct node{  T data  node* next;};


Then an iterator just holds a node object really.

template < class T >class iterator{  iterator( node<T>* thenode)    : n(thenode)  {}  node* next;  iterator& operator ++ ();  iterator operator ++ (int)  T& operator * ();};


and the function to create one just returns either the root node or the last node's next pointer (1 passed the end of the list). How you get a hold of those pointers depends on how you've implemented the lisnked list structure.

iterator list::begin(){   return iterator(root_node)}iterator list::end(){  return iterator(end_node->next);}
[size=2]aliak.net
since a iterator is just a point, it the template nessary?
Quote:Original post by 3dmodelerguy
since a iterator is just a point, it the template nessary?

Yes, as well as having the correct type for the pointer to the node itself, it is necessary for the implementation of the operators * and ->.

This topic is closed to new replies.

Advertisement