Jump to content
  • Advertisement
Sign in to follow this  

creating iterators

This topic is 4747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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);
}

Share this post


Link to post
Share on other sites
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 ->.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!