hash_map::difference_type

说明说明

此 API 已过时。另一种方法是 unordered_map Class

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

typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::difference_type difference_type;

备注

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

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

在 Visual C++ .NET 2003 中,<hash_map><hash_set> 标头文件的成员中不再标准,命名空间,而是将 stdext 命名空间。有关更多信息,请参见 stdext 命名空间

示例

// hash_map_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <hash_map>
#include <algorithm>

int main( )
{
   using namespace std;
   using namespace stdext;
   hash_map <int, int> hm1;
   typedef pair <int, int> Int_Pair;

   hm1.insert ( Int_Pair ( 2, 20 ) );
   hm1.insert ( Int_Pair ( 1, 10 ) );
   hm1.insert ( Int_Pair ( 3, 20 ) );

   // The following won't insert, because map keys are unique
   hm1.insert ( Int_Pair ( 2, 30 ) );   

   hash_map <int, int>::iterator hm1_Iter, hm1_bIter, hm1_eIter;   
   hm1_bIter = hm1.begin( );
   hm1_eIter = hm1.end( );

   // Count the number of elements in a hash_map 
   hash_map <int, int>::difference_type  df_count = 0;
   hm1_Iter = hm1.begin( );
   while ( hm1_Iter != hm1_eIter)   
   {
      df_count++;
      hm1_Iter++;
   }

   cout << "The number of elements in the hash_map hm1 is: " 
        << df_count << "." << endl;

   cout  << "The keys of the mapped elements are:";
   for ( hm1_Iter= hm1.begin( ) ; hm1_Iter!= hm1.end( ) ;
         hm1_Iter++)
      cout << " " << hm1_Iter-> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are:";
   for ( hm1_Iter= hm1.begin( ) ; hm1_Iter!= hm1.end( ) ;
         hm1_Iter++)
      cout << " " << hm1_Iter-> second;
   cout << "." << endl;
}
  
  
  

要求

标头: <hash_map>

命名空间: stdext

请参见

参考

hash_map Class

标准模板库