set::difference_type
可用来表示一组元素的数目。一范围元素间的有符号整数类型指向的迭代器。
typedef typename allocator_type::difference_type difference_type;
备注
在减去或增长。容器的迭代器,则 difference_type 是返回的类型。 difference_type 通常用于表示元素数处于范围 [_First,) 的_Last 在迭代器 _First 之间,并且 _Last元素,包括指向由 _First 和范围元素,但不包括,元素指向的 _Last。
请注意,虽然 difference_type 提供满足要求,输入迭代器包含用于双向迭代器类。可逆容器支持如集的所有迭代器,在迭代器之间的减法可由一个随机访问容器提供的随机访问迭代器只支持。矢量。
示例
// set_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <set>
#include <algorithm>
int main( )
{
using namespace std;
set <int> s1;
set <int>::iterator s1_Iter, s1_bIter, s1_eIter;
s1.insert( 20 );
s1.insert( 10 );
s1.insert( 20 ); // won't insert as set elements are unique
s1_bIter = s1.begin( );
s1_eIter = s1.end( );
set <int>::difference_type df_typ5, df_typ10, df_typ20;
df_typ5 = count( s1_bIter, s1_eIter, 5 );
df_typ10 = count( s1_bIter, s1_eIter, 10 );
df_typ20 = count( s1_bIter, s1_eIter, 20 );
// the keys, and hence the elements of a set are unique,
// so there is at most one of a given value
cout << "The number '5' occurs " << df_typ5
<< " times in set s1.\n";
cout << "The number '10' occurs " << df_typ10
<< " times in set s1.\n";
cout << "The number '20' occurs " << df_typ20
<< " times in set s1.\n";
// count the number of elements in a set
set <int>::difference_type df_count = 0;
s1_Iter = s1.begin( );
while ( s1_Iter != s1_eIter)
{
df_count++;
s1_Iter++;
}
cout << "The number of elements in the set s1 is: "
<< df_count << "." << endl;
}
要求
标头: <set>
命名空间: std