An iterator is an object that permits a programmer to easily step through a data structure (such as a linked list, stack, or queue). They allow you to examine, possibly even change an item in the data structure.
These are similar to subscripts in that they provide access to a specific element in a data structure. But they differ from subscripts in that they are implementation independent.
An iterator class is implemented as a template class, and it usually contains the following methods/data members:
begin()
This method returns an iterator to the first item in the data structure
To use this method: iterator_name = object_name.begin();
end()
This method returns an iterator to the last item in the data structure
To use this method: iterator_name = object_name.end();
the * operator
This operator can be used to get/change the value in the data structure at a specific position.
To use this operator: *iterator_name
the ++ operator
This operator can be used to move an iterator to the next item in the data structure. It can be used in both a pre and post increment fashion. Take care when using this operator because it does not check to make sure that end() has not been passed.
To use this operator: ++iterator_name;
To use this operator: iterator_name++;
the -- operator
This operator can be used to move an iterator to the previous item in the data structure. It can be used in both a pre and post decrement fashion. Take care when using this operator because it does not check to make sure that begin() has not been passed.
To use this operator: --iterator_name;
To use this operator: iterator_name--;
the == and != operators
The == operator can be used to determine if two iterators are referring to the same position in a data structure. The != operator can be used to determine if two iterators are not referring to the same position in a data structure. Both of these operators can ONLY be used if the two iterators refer to the same data structure.
To use this operator: iterator_name_1 == iterator_name_2
To use this operator: iterator_name_1 != iterator_name_2