Demonstrate advance() and distance() in list
#include <iostream>
#include <list>
#include <iterator>
using namespace std;
int main()
{
list<char> listObject;
list<char>::iterator p;
int i;
for(i = 0; i <10; i++)
listObject.push_back('A'+i);
p = listObject.begin();
cout << "Character at p: " << *p << endl;
// move two characters forward using advance()
advance(p, 2);
cout << "Character at p+2: " << *p << endl;
cout << "Number of elements from listObject.begin() ";
cout << "to listObject.end(): ";
cout << distance(listObject.begin(), listObject.end());
return 0;
}
Related examples in the same category