C++ STL advance

迭代器前进或者后退n个元素



#include <iterator>
#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

int main()
{
list<int> coll;

for (int i=0;i<=9;++i)
{
coll.push_back(i);
}

list<int>::iterator pos = coll.begin();
//print actual element
cout << *pos << endl;

//step three elements forword
advance(pos,3);

//print actual element
cout << *pos << endl;

//step one element backword
advance(pos,-1);

//print
cout << *pos << endl;


system("pause");
}


运行结果

0

3

2

请按任意键继续. . .