Namespaces
Variants
Views
Actions

std::advance

From cppreference.com
Defined in header <iterator>
template< class InputIterator, class Distance >
void advance( InputIterator& it, Distance n );

Increments (or decrements if n < 0) given iterator it by n elements.

For random access iterators constant time is required, because operator+ and operator- are used.

Linear time is required for input, forward or bidirectional iterators, since for them operator++ and operator-- are used.

Contents

[edit] Parameters

it - iterator to be advanced
n - number of elements it should be advanced. Shall only be negative if it is either a bidirectional or random access iterator.

[edit] Return value

(none)

[edit] Example

#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
 
    auto vi = v.begin();
 
    std::advance(vi, 2);
 
    std::cout << *vi << '\n';
}

Output:

4

[edit] See also

returns the distance between two iterators
(function) [edit]