set::difference_type
可以用來表示一組裝置的元件數目一個範圍的項目之間的帶正負號的整數類資料型別指向 Iterator。
typedef typename allocator_type::difference_type difference_type;
備註
當減去或將透過容器的 Iterator 時, difference_type 會傳回的型別。 difference_type 通常用來表示項目數範圍 [_First, _Last) 在 Iterator _First , _Last,包含這個項目指向 _First 和項目的範圍,,但是,這個項目所指向的 _Last。
請注意,雖然 difference_type 為符合輸入 Iterator 需求,包括雙向 Iterator 類別由復原容器支援例如集合中所有的 Iterator,在 Iterator 之間的減法可由隨機存取容器提供的隨機存取 Iterator 只支援例如向量。
範例
// 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