std::advance - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::advance

From cppreference.com
< cpp‎ | iterator

Defined in header `<iterator>` |  |
template< class InputIt, class Distance > void advance( InputIt& it, Distance n ); |  |

Increments given iterator `it` by `n` elements.

If `n` is negative, the iterator is decremented. In this case, `InputIt` must meet the requirements of `BidirectionalIterator`, otherwise the behavior is undefined.

### [edit] Parameters

it | - | iterator to be advanced
n | - | number of elements `it` should be advanced
Type requirements
- `InputIt` must meet the requirements of `InputIterator`.

### [edit] Return value

(none)

### [edit] Complexity

Linear.

However, if `InputIt` additionally meets the requirements of `RandomAccessIterator`, complexity is constant.

### [edit] Example

Run this code

    #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

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/advance&oldid=57955"