Iterators

An iterator is an object that is used to reference an element stored in a container. There are 5 types of iterators:

An input iterator is used to read data from an input stream. It steps forward element by element and returns the values element by element. The operations that can be performed on input iterators are:

Expression Effect
*inputIter gives access to the element to which inputIter refers
inputIter -> member gives access to the specific member of the element
++inputIter (pre-increment) moves forward and returns the new position
inputIter++ (post-increment) moves forward and returns the old position
inputIter1 == inputIter2 returns a boolean value indicating if the 2 iterators are the same
inputIter1 != inputIter2 returns a boolean value indicating if the 2 iterators are not the same

An output iterator is used to write data to an output stream. It steps forward element by element.

Output iterators cannot be used to iterate over a range twice. Therefore, it data is written at the same position twice, there is no guarantee that the new value will replace the old value.

The operations that can be performed on output iterators are:

Expression Effect
*outputIter = value; writes the value at the position specified by outputIter
++outputIter (pre-increment) moves forward and returns the new position
outputIter++ (post-increment) moves forward and returns the old position

A forward iterator combines the functionality of the input and output iterators.

Forward iterators can refer to the same element in the same collection and process the same element more than once.

The operations that can be performed on forward iterators are:

Expression Effect
*forwardIter gives access to the element to which forwardIter refers
forwardIter -> member gives access to the specific member of the element
++forwardIter (pre-increment) moves forward and returns the new position
forwardIter++ (post-increment) moves forward and returns the old position
forwardIter1 == forwardIter2 returns a boolean value indicating if the 2 iterators are the same
forwardIter1 != forwardIter2 returns a boolean value indicating if the 2 iterators are not the same
forwardIter1 = forwardIter2 assigns forwardIter2 to forwardIter1

A bidirectional iterator is a forward iterator that can also iterate backward over the elements.

This type of iterator can used with the sequence and associative containers.

The operations that can be performed on bidirectional iterators include those listed for the forward iterators and:

Expression Effect
--backwardIter (pre-decrement) moves backward and returns the new position
backwardIter-- (post-decrement) moves backward and returns the old position

A random access iterator is a bidirectional iterator that can be used to randomly access the elements of a container.

This type of iterator can used with the sequence containers.

The operations that can be performed on random access iterators include those listed for the bidirectional iterators and:

Expression Effect
randomIter[n] access the nth element
randomIter += n moves the iterator forward n elements if n is positive or backward n elements if n is negative
randomIter -= n moves the iterator backward n elements if n is positive or forward n elements if n is negative
randomIter + n returns the iterator of the next nth element
n + randomIter returns the iterator of the next nth element
randomIter - n returns the iterator of the previous nth element
randomIter1 - randomIter2 returns the distance between the 2 iterators
randomIter1 < randomIter2 return a boolean value indicating if randomIter1 is before randomIter2
randomIter1 <= randomIter2 return a boolean value indicating if randomIter1 is before or equal to randomIter2
randomIter1 > randomIter2 return a boolean value indicating if randomIter1 is after randomIter2
randomIter1 >= randomIter2 return a boolean value indicating if randomIter1 is after or equal to randomIter2

Creating Iterators

Constructor Description
container_type::iterator iterator_name; this creates an iterator for a specific container
container_type::const_iterator iterator_name; these are used when a container is declared to be constant in order to prevent the iterator from modifying the elements of the container.
istream_iterator<Type> iterator_name( istream & ); these are used to input data into a program from an input stream. Type is either a built-in type or user defined type for which an input iterator is defined.
ostream_iterator<Type> iterator_name( ostream & ); these are used to output data from a program into an output stream. Type is either a built-in type or user defined type for which an output iterator is defined.
ostream_iterator<Type> iterator_name( ostream &, char *separator ); these are used to output data from a program into an output stream. Type is either a built-in type or user defined type for which an output iterator is defined. separator is the character that will be used to separate the output.

All of the container classes contain the following methods that use or set an iterator:

Method Description
begin returns an iterator to the first element in the container. To use this method: iterator_name = container_name.begin();
end returns an iterator to the position after the last element in the container. To use this method: iterator_name = container_name.end();
insert(iterPosition, value) inserts value in the container at the position specified by iterPosition. To use this method: container_name.insert( beg, end);
erase( beginIter, endIter) deletes all of the elements between beginIter and endIter - 1 from the container.  To use this method: container_name.erase( beg, end);