Input Iterator is an iterator used to read the values from the container. Dereferencing an input iterator allows us to retrieve the value from the container. It does not alter the value of a container. It is a one-way iterator. It can be incremented, but cannot be decremented.
Iterators are used to traverse from one element to another element, a process is known as iterating through the container. The main advantage of an iterator is to provide a common interface for all the containers type. Iterators make the algorithm independent of the type of the container used.
I was reading Stanley Lippman's book C++ Primer to learn more about C++ 11.
In the chapter on Generic Algorithms he mentions that iterators used in the generic algorithms can be classified into 5 types based on the operations they support : input iterators, output iterators, forward iterators, bidirectional iterators and random access iterators.
Quote from his book:
Input iterators can read elements in a sequence. They must provide the following operators - equality (
==
), inequality (!=
), dereference (*
), postfix&
prefix increment (++
) and the arrow operator (->
). Input iterators may be used only sequentially. We are guaranteed that*it++
is valid, but incrementing an input iterator may invalidate all other iterators on that stream. As a result there is no guarantee that we can save the state of an input iterator and examine an element through that saved iterator
I have trouble understanding the quote in bold. Why would incrementing an input iterator which is meant only for reading elements invalidate other iterators? Why cant we save the state of an input iterator?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With