vector::difference_type
提供參考相同向量中項目之兩個迭代器間差異的類型。
typedef typename Allocator::difference_type difference_type;
備註
difference_type 也可以兩個指標之間的項目數來描述。
迭代器通常可用來存取向量項目。
範例
// 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