Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

2.5 Iterator Operations

The C++ Standard Library provides two functions that can be used to manipulate iterators. The function std::advance() takes an iterator and a numeric value as argument, and modifies the iterator by moving the given amount.

For random access iterators this is the same as iter + n; however, the function is useful because it is designed to operate with all forms of iterators. For forward iterators the numeric distance must be positive, whereas for bidirectional or random access iterators the value can be either positive or negative. The operation is efficient (constant time) only for random access iterators. In all other cases, it is implemented as a loop that invokes either operator++() or operator--() on the iterator, and therefore takes time proportional to the distance traveled. The std::advance() function does not check to ensure the validity of the operations on the underlying iterator.

The second function, std::distance(), returns the number of iterator operations necessary to move from one element in a sequence to another. The description of this function is as follows:

The result returned is the number of times that first must be incremented or decremented in order for (first == last) to equal true. The function requires that last be reachable from first.


NOTE -- The above definition of distance assumes that your compiler supports partial specialization. If it does not, then you must use the following alternate definition:
template <class InputIterator, class Distance>
void distance(InputIterator first, InputIterator last, Distance& n);



Previous fileTop of DocumentContentsIndex pageNext file