STL中的distance和advance的简单用法

 

复制代码
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    list<int> coll;

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

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

    advance(pos, 3);
    cout << *pos << endl;

    advance(pos, -1);
    cout << *pos << endl;

    int size = distance(coll.begin(), coll.end());

    printf("size is %d\n", size);

    getchar();
    return 0;
}
复制代码

运行结果:

参考:http://www.programlife.net/stl-iterator-funcation-advance.html

     http://www.programlife.net/stl-iterator-funcation-distance.html

posted @   懒猫的新窝  阅读(1407)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示