cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : iterator : advance
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
iterator
advance
back_inserter
distance
front_inserter
inserter
iterator
iterator_traits
iterator categories:
· BidirectionalIterator
· ForwardIterator
· InputIterator
· OutputIterator
· RandomAccessIterator
predefined iterators:
· back_insert_iterator
· front_insert_iterator
· insert_iterator
· istreambuf_iterator
· istream_iterator
· ostreambuf_iterator
· ostream_iterator
· reverse_iterator

-

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

Advance iterator

Advances the iterator i by n elements.

If i is a Random Access Iterator, the function uses once operator+ or operator-, otherwise, the function uses repeatedly the increase or decrease operator (operator++ or operator--) until n elements have been advanced.

Parameters

i
Iterator to be advanced.
n
Number of elements to advance.
This can only be negative for random access and bidirectional iterators.
Distance is any numerical type able to represent distances between iterators of this type.

Return value

none

Example

// advance example
#include <iostream>
#include <iterator>
#include <list>
using namespace std;

int main () {
  list<int> mylist;
  for (int i=0; i<10; i++) mylist.push_back (i*10);

  list<int>::iterator it = mylist.begin();

  advance (it,5);

  cout << "The sixth element in mylist is: " << *it << endl;

  return 0;
}

Output


The sixth element in mylist is: 50

Complexity

Constant for random access iterators.
Linear on n for other categories of iterators.

See also

distance Return distance between iterators (function template)
iterator Iterator definitions (header)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us