advance


Change the current position of an iterator.

Increment the iterator reference i by n . The variable n can be negative, meaning decrement instead of increment, for random access and bidirectional iterators only.

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class InputIterator, class Distance >
void advance
  (
  InputIterator& i,
  Distance n
  );
  

Complexity

Time complexity is constant for random access iterators and linear for all other iterators. Space complexity is constant.

Example <ospace/osstd/examples/advance.cpp>
#include <iostream>
#include <iterator>
#include <list>

void
main()
  {
  list< int > v;
  for ( int i = 0; i < 10; ++i )
    v.push_back( 100 + i );
  list< int > ::iterator location = v.begin();
  cout << "first: " << *location << "\n";

  advance( location, 8 );
  cout << "first + 8: " << *location << "\n";

  advance( location, -3 );
  cout << "first + 5: " << *location << "\n";
  }

first: 100
first + 8: 108
first + 5: 105

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.