hash_map::difference_type

备注

此 API 已过时。替代为 unordered_map 类

可用来表示一 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 提供满足要求,输入迭代器包含用于双向迭代器类。可逆容器支持如集的所有迭代器,在迭代器之间的减法可由一个随机访问容器提供的随机访问迭代器只支持,如矢量。

在Visual C++ .NET 2003中,成员<hash_map><hash_set> 头文件不再在std命名空间,而是已经进入了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 类

标准模板库