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

 

相关文章:

  • 2021-07-24
  • 2021-12-16
  • 2022-03-09
  • 2021-11-18
  • 2021-10-19
  • 2021-04-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2022-02-10
  • 2021-05-08
  • 2021-05-07
相关资源
相似解决方案