In page 91 of the book Elements of Programming, Stepanov and McJones say that the concept of Iterator requires a successor
function but that is not necessarily regular because
...
i = j
does not imply thatsuccessor(i) = successor(j)
...
(see page online)
I understand the converse successor(i) = successor(j)
does not imply i=j
(for example in two null terminated list) and that successor
function may not be defined for some inputs. But I don't understand how could it be possible that i = j
can lead to successor(i) != successor(j)
.
What case would they be referring to? Perhaps some iterator that does random (as in aleatory) hops? or some iterator that has a hidden state and "jumps" differently than the other iterator after pointing to the same element (and comparing equal in this sense).
They immediately jump to refinements (ForwardIterator) that require a regular successor
function so it is not clear to me.
Initially I thought that an input iterator can have this property. However is still difficult to me to see if this constitutes a counterexample: (within a certain implementation of STL).
#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)
*it1 != *it2
, maybe it is an implementation problem. (see the new code in my equation). I know I shouldn't compare the axiomatic book of Stepanov with a mundane implementation of STL, but I wonder if in practice the ideals still hold in a practice. – Cardiogram