map::difference_type

可用于表示映射的元素数在一个范围的组件之间的一个带符号整数类型指向由迭代器。

typedef allocator_type::difference_type difference_type;

备注

在减去或增加通过容器的迭代器时,difference_type 是返回的类型。difference_type 通常用于表示元素数。范围 [_First,_Last)的 迭代器在 _First 之间,并 _Last,包括元素指向由 _First 和元素的大小,但不包括,元素指向由 _Last。

请注意,尽管 difference_type 未满足输入迭代的要求,包括双向迭代器选件类由可逆容器支持例如set所有迭代器,在迭代之间的减法可用由一个随机访问容器提供的随机访问迭代器仅支持例如矢量。

示例

// 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

请参见

参考

map Class

标准模板库