Iterator operations – HP Integrity NonStop H-Series User Manual
Page 36
![background image](/manuals/396950/36/background.png)
Click on the banner to return to the user guide home page.
©Copyright 1996 Rogue Wave Software
Iterator Operations
The standard 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.
void advance (InputIterator & iter, Distance & n);
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 iterator. 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:
void distance (InputIterator first, InputIterator last,
Distance &n);
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.