vector::difference_type

A type that provides the difference between two iterators that refer to elements within the same vector.

typedef typename Allocator::difference_type difference_type;

Remarks

A difference_type can also be described as the number of elements between two pointers.

An iterator is more commonly used to access a vector element.

Example

// 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;
}}
The value 46 occurs 3 elements after the value 96.

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library