可用于表示一个列表的元素数在一个范围的组件之间的一个带符号整数类型指向由迭代器。
typedef typename Allocator::difference_type difference_type;
备注
在减去或增加通过容器的迭代器时,difference_type 是返回的类型。difference_type 通常用于表示元素数。范围[_First,_Last)的迭代器在 _First 之间,并 _Last,包括元素指向由 _First 和元素的大小,但不包括,元素指向由 _Last。
请注意,尽管 difference_type 未满足输入迭代的要求,包括双向迭代器选件类由与设置的可逆容器支持的所有迭代器,在迭代之间的减法可用由一个随机访问的容器提供的随机访问迭代器只支持,例如 向量选件类。
示例
// list_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <list>
#include <algorithm>
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter, c2_Iter;
c1.push_back( 30 );
c1.push_back( 20 );
c1.push_back( 30 );
c1.push_back( 10 );
c1.push_back( 30 );
c1.push_back( 20 );
c1_Iter = c1.begin( );
c2_Iter = c1.end( );
list <int>::difference_type df_typ1, df_typ2, df_typ3;
df_typ1 = count( c1_Iter, c2_Iter, 10 );
df_typ2 = count( c1_Iter, c2_Iter, 20 );
df_typ3 = count( c1_Iter, c2_Iter, 30 );
cout << "The number '10' is in c1 collection " << df_typ1 << " times.\n";
cout << "The number '20' is in c1 collection " << df_typ2 << " times.\n";
cout << "The number '30' is in c1 collection " << df_typ3 << " times.\n";
}
要求
标头: <list>
命名空间: std