Advertisement Jump to content
Sign in to follow this  
3dmodelerguy

creating iterators

This topic is 4987 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
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.

Share this post


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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!