C++ distance()处理迭代器之间的距离


C++ distance() 处理迭代器之间的距离

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

using namespace std;

int main()
{
    list<int> list1;
    for (int k=-3;k<=9;++k)
    {
        list1.push_back(k);
    }

    list<int>::iterator pos1;
    pos1 = find(list1.begin(),list1.end(),5);

    if (pos1 != list1.end())
    {
        cout << "difference between begining and 5:  " << distance(list1.begin(),pos1) << endl;
    }
    else
    {
        cout << "element 5 not find..." << endl;
    }

    system("pause");
    return 0;
}

difference between begining and 5: 8
请按任意键继续. . .

 

代码参考:C++标准库(第2版)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM