Input Iterators

An input iterator has many expected qualities — comparable, dereferenceable, incrementable — that are commonly associated with iterators. The important distinction that is often missed is that an input iterator may support only single-pass algorithms. SGI (http://www.sgi.com/tech/stl/InputIterator.html) states that "after executing ++i, it is not required that copies of the old value of i be dereferenceable or that they be in the domain of operator ==()" and "it is not guaranteed that it is possible to pass through the same input iterator twice." It is this single-pass nature that is an artifact of the technique described here. Therefore, conducting an inner iteration within the outer would lead to unintended behavior, as in:


container::iterator begin(c.begin());
container::iterator end(c.end());
for(; begin != end; ++begin)
{
  if(xyz(*begin))
  {
    container::iterator it = find_if(begin, end, . . .);
    ...
  }

}

The find_if() loop would cause the outer iteration to be advanced, thereby failing to do the search in the desired manner.

Back to Article