可用于表示多个集的元素数在一个范围的组件之间的一个带符号整数类型指向由迭代器。
typedef typename allocator_type::difference_type difference_type;
备注
在减去或增加通过容器的迭代器时,difference_type 是返回的类型。difference_type 通常用于表示元素数。范围[_First,_Last)的迭代器在 _First 之间,并 _Last,包括元素指向由 _First 和元素的大小,但不包括,元素指向由 _Last。
请注意,尽管 difference_type 未满足输入迭代的要求,包括双向迭代器选件类由与设置的可逆容器支持的所有迭代器,在迭代之间的减法用于受与矢量的一个随机访问的容器提供的随机访问迭代器只支持。
示例
// multiset_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <set>
#include <algorithm>
int main( )
{
using namespace std;
multiset <int> ms1;
multiset <int>::iterator ms1_Iter, ms1_bIter, ms1_eIter;
ms1.insert( 20 );
ms1.insert( 10 );
ms1.insert( 20 );
ms1_bIter = ms1.begin( );
ms1_eIter = ms1.end( );
multiset <int>::difference_type df_typ5, df_typ10, df_typ20;
df_typ5 = count( ms1_bIter, ms1_eIter, 5 );
df_typ10 = count( ms1_bIter, ms1_eIter, 10 );
df_typ20 = count( ms1_bIter, ms1_eIter, 20 );
// The keys, and hence the elements, of a multiset are not unique
cout << "The number '5' occurs " << df_typ5
<< " times in multiset ms1.\n";
cout << "The number '10' occurs " << df_typ10
<< " times in multiset ms1.\n";
cout << "The number '20' occurs " << df_typ20
<< " times in multiset ms1.\n";
// Count the number of elements in a multiset
multiset <int>::difference_type df_count = 0;
ms1_Iter = ms1.begin( );
while ( ms1_Iter != ms1_eIter)
{
df_count++;
ms1_Iter++;
}
cout << "The number of elements in the multiset ms1 is: "
<< df_count << "." << endl;
}
要求
标头: <set>
命名空间: std