On page 91 of the Programming Elements book, Stepanov and McJones say that the Iterator concept requires the successor
function, but this is not necessary regularly because
... i = j
does not mean that successor(i) = successor(j)
...
(see web page )
I understand that the reverse successor(i) = successor(j)
does not mean i=j
(for example, in two lists with zero completion) and that the successor
function cannot be defined for some inputs. But I do not understand how it is possible that i = j
can lead to successor(i) != successor(j)
.
In which case will they be referenced? Perhaps some kind of iterator that makes random (as in aleatory) hops? or some iterator that has a hidden state and βjumpsβ differently than another iterator, pointing to the same element (and comparing in this sense).
They immediately proceed to refinements (ForwardIterator), which require the regular successor
function, so this is not clear to me.
Initially, I thought that an input iterator could have this property. However, itβs still hard for me to understand if this is a counterexample: (within the framework of a specific STL implementation).
#include <iostream> #include <sstream> #include <iterator> #include <numeric> #include <cassert> using std::cout; using std::endl; int main(){ std::istream_iterator<int> it1(std::cin); // wait for one input std::istream_iterator<int> it2 = it1; assert(it1 == it2); cout << "*it1 = " << *it1 << endl; cout << "*it2 = " << *it2 << endl; cout << "now sucessor" << endl; ++it1; // wait for one input ++it2; // wait for another input assert(it1 == it2); // inputs still compare equal ! cout << "*it1 = " << *it1 << endl; cout << "*it2 = " << *it2 << endl; assert(it1 == it2); // also here ! and yet they point to different values... assert(*it1 == *it2); // assert fails! }
(compiled with GCC 6.1)
source share