Standard C++ Library Class Reference
decrements reference i. Remember that advance accepts a negative argument n for random
access and bidirectional iterators only.
Example
//
// advance.cpp
//
#include<iterator>
#include<list>
#include<iostream.h>
int main()
{
//
//Initialize a list using an array
//
int arr[6] = {3,4,5,6,7,8};
list<int> l(arr,arr+6);
//
//Declare a list iterator, s.b. a ForwardIterator
//
list<int>::iterator itr = l.begin();
//
//Output the original list
//
cout << "For the list: ";
copy(l.begin(),l.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "When the iterator is initialized to l.begin(),"
<< endl << "it points to " << *itr << endl << endl;
//
// operator+ is not available for a ForwardIterator,
// so use advance.
//
advance(itr, 4);
cout << "After advance(itr,4), the iterator points to "
<< *itr << endl;
return 0;
}
Output :
For the list: 3 4 5 6 7 8
When the iterator is initialized to l.begin(),
it points to 3
After advance(itr,4), the iterator points to 7