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