Namespaces
Variants
Views
Actions

std::advance

From cppreference.com
< cpp‎ | iterator
Revision as of 16:30, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <iterator>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class InputIt, class Distance >
void advance( InputIt& it, Distance n );
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

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.

Contents

Parameters

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

Return value

(none)

Complexity

Linear.

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

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

See also

Template:cpp/iterator/dcl list distance