C++ STL advance

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
请按任意键继续. . .

 

posted @ 2020-10-04 21:04  西北逍遥  阅读(175)  评论(0编辑  收藏  举报