vector::difference_type
동일한 벡터 내의 요소를 참조하는 두 반복기 간의 차이를 제공하는 형식입니다.
typedef typename Allocator::difference_type difference_type;
설명
difference_type은 두 포인터 사이의 요소로도 설명할 수 있습니다.
iterator는 벡터 요소에 액세스하는 데 사용되는 경우가 더 많습니다.
예제
// vector_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int> vec;
vector <int>::iterator it_1, it_2;
// A vector with non-sequential values
vec.push_back(21);
vec.push_back(26);
vec.push_back(98);
vec.push_back(4);
vec.push_back(79);
vec.push_back(46);
vec.push_back(69);
// How far apart are 98 and 46 in the vector?
// (Making several assumptions for the sake of simplicity.)
it_1 = std::find(vec.begin(), vec.end(), 98);
it_2 = std::find(vec.begin(), vec.end(), 46);
vector <int>::difference_type distance = it_2 - it_1;
cout << "The value 46 occurs " << distance << " elements after the value 96." << endl;
}}
요구 사항
헤더: <vector>
네임스페이스: std