An iterator is a design pattern, but interestingly, the STL work started much before
Gang of Four published their design patterns-related work to the software community. Iterators themselves are objects that allow traversing the containers to access, modify, and manipulate the data stored in the containers. Iterators do this so magically that we don't realize or need to know where and how the data is stored and retrieved.
The following image visually represents an iterator:
From the preceding image, you can understand that every iterator supports the begin() API, which returns the first element position, and the end() API returns one position past the last element in the container.
The STL broadly supports the following five types of iterators:
- Input iterators
- Output iterators
- Forward iterators
- Bidirectional iterators
- Random-access iterators
The container implements the iterator to let us easily retrieve and manipulate the data, without delving much into the technical details of a container.
The following table explains each of the five iterators:
|
The type of iterator
|
Description
|
|
Input iterator
|
- It is used to read from the pointed element
- It is valid for single-time navigation, and once it reaches the end of the container, the iterator will be invalidated
- It supports pre- and post-increment operators
- It does not support decrement operators
- It supports dereferencing
- It supports the == and != operators to compare with the other iterators
- The istream_iterator iterator is an input iterator
- All the containers support this iterator
|
|
Output iterator
|
- It is used to modify the pointed element
- It is valid for single-time navigation, and once it reaches the end of the container, the iterator will be invalidated
- It supports pre- and post-increment operators
- It does not support decrement operators
- It supports dereferencing
- It doesn't support the == and != operators
- The ostream_iterator, back_inserter, front_inserter iterators are examples of output iterators
- All the containers support this iterator
|
|
Forward iterator
|
- It supports the input iterator and output iterator functionalities
- It allows multi-pass navigation
- It supports pre-increment and post-increment operators
- It supports dereferencing
- The forward_list container supports forward iterators
|
|
Bidirectional iterator
|
- It is a forward iterator that supports navigation in both directions
- It allows multi-pass navigation
- It supports pre-increment and post-increment operators
- It supports pre-decrement and post-decrement operators
- It supports dereferencing
- It supports the [] operator
- The list, set, map, multiset, and multimap containers support bidirectional iterators
|
|
Random-access iterator
|
- Elements can be accessed using an arbitrary offset position
- It supports pre-increment and post-increment operators
- It supports pre-decrement and post-decrement operators
- It supports dereferencing
- It is the most functionally complete iterator, as it supports all the functionalities of the other types of iterators listed previously
- The array, vector, and deque containers support random-access iterators
- A container that supports random access will naturally support bidirectional and other types of iterators
|