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

请参见

参考

vector 类

标准模板库