map::difference_type
可以用來表示對應元素數目某個範圍的項目之間的帶正負號的整數 (Unsigned Integer) 型別指向的 Iterator。
typedef allocator_type::difference_type difference_type;
備註
,當減去或加入應用程式的 Iterator 時, difference_type 為傳回的型別。 difference_type 通常用來表示項目數目範圍 [_First, _Last) 。 _First Iterator 之間,並 _Last,包括項目指向 _First 和項目的範圍,,但不包含項目,指向 _Last。
請注意,雖然為符合輸入 Iterator 需求,包括雙向 Iterator 可用於此類別是由容器支援 (如集合的所有 Iterator,在 Iterator 之間的減法是一種適用於隨機存取容器所提供的隨機存取 Iterator 只支援 difference_type 例如向量。
範例
// map_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <map>
#include <algorithm>
int main( )
{
using namespace std;
map <int, int> m1;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 3, 20 ) );
m1.insert ( Int_Pair ( 2, 30 ) );
map <int, int>::iterator m1_Iter, m1_bIter, m1_eIter;
m1_bIter = m1.begin( );
m1_eIter = m1.end( );
// Count the number of elements in a map
map <int, int>::difference_type df_count = 1;
m1_Iter = m1.begin( );
while ( m1_Iter != m1_eIter)
{
df_count++;
m1_Iter++;
}
cout << "The number of elements in the map m1 is: "
<< df_count << "." << endl;
}
需求
標題: <map>
命名空間: std