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版)

原文地址:https://www.cnblogs.com/herd/p/12112784.html