iterator_distance.cpp

来自「C++ sample code, for the book: C++ blac」· C++ 代码 · 共 29 行

CPP
29
字号

#include<iterator>
#include<vector>
#include <iostream>
using namespace std;

const int MAX = 4;

int main()
{
    int numbers_array[MAX];
    vector<int> numbers_vector(MAX);

    for (int loop_index = 0; loop_index < MAX; loop_index++){
        numbers_array[loop_index] = loop_index;
    }

    copy(numbers_array, numbers_array + MAX, numbers_vector.begin());

    vector<int>::iterator itr = numbers_vector.begin() + 2;

    vector<int>::difference_type itr_distance;
    distance(numbers_vector.begin(), itr, itr_distance);

    cout << "The distance from the beginning to the iterator is: " 
        << itr_distance << endl;

   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?