Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

2.5 Iterator Operations

The Standard C++ Library provides two functions that can be used to manipulate iterators. The function 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 the operators ++ or -- on the iterator, and therefore takes time proportional to the distance traveled. The advance() function does not check to ensure the validity of the operations on the underlying iterator.

The second function, 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 is returned in the third argument, which is passed by reference. Distance will increment this value by the number of times the operator ++ must be executed to move from first to last. Always be sure that the variable passed through this argument is properly initialized before invoking the function.


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:
void distance (InputIterator first, InputIterator last, Distance &n);



Previous fileTop of DocumentContentsIndexNext file

OEM Edition, OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.