std::advance

Header: <iterator>

Increments given iterator it by n elements.

# Declarations

template< class InputIt, class Distance >
void advance( InputIt& it, Distance n );

(until C++17)

template< class InputIt, class Distance >
constexpr void advance( InputIt& it, Distance n );

(since C++17)

# Parameters

# Return value

(none)

# Notes

The behavior is undefined if the specified sequence of increments or decrements would require that a non-incrementable iterator (such as the past-the-end iterator) is incremented, or that a non-decrementable iterator (such as the front iterator or the singular iterator) is decremented.

# 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 << ' ';
 
    vi = v.end();
    std::advance(vi, -2);
    std::cout << *vi << '\n';
}

# See also