The advantages to an iterator--over say a simpler mechanism, like say a pointer--is that they can provide an interface for performing operations on the object they refer or can provide an interface for accessing other objects in the container.
So for example an iterator might have a Delete() member function that cn be used to delete the object from the container/collection. Or it might have an operator ++() member that can be used to access the next item in the container's sequence.
Another advantage of iterators, which actually depends in part on these advantages, is that they can be used to write generic algorithms for manipulating items in containers/collection that do not depend on the nature of the container/collection. For example you could write a sort() algoprithm that sorts all the items in a container when passed an iterator to the container's elements. That single sort algorithm may be able to sort containers that use greatly different implimentations, for storing the items, like say dynamic arrays or linked lists, because the iterators provide a consistent interface to the container's items. for example the iterator's operator ++() (or something like it) could always be used to get from one item to the next, even though the process for doing so is greatly different depending on the container's implimentation.
continues
Main Topics
Browse All Topics





by: nietodPosted on 2000-07-08 at 04:13:19ID: 3246134
An iterator is a object that is used to access an element from a container or collection of objects. It is in many ways like a pointer in that it is a an object that refers to another object. The object it refers to however, is somehow managed by another object, like a container, and the iterator is provided by this managing object to let you access the managed object.
continues