共用方式為


hash_map::difference_type

注意事項注意事項

這個 API 已經過時。這個選項是 unordered_map Class

可以用來表示 hash_map 的元素數目一個範圍的項目之間的帶正負號的整數型別指向的 Iterator。

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

備註

當減去或將透過容器的 Iterator 時, difference_type 為傳回的型別。 difference_type 通常用來表示項目數範圍 [_First, _Last)_First Iterator之間,並 _Last,包括項目指向 _First 和項目範圍,但不包括,,項目指向 _Last。

請注意,雖然 difference_type 為符合輸入 Iterator 需求,包括雙向 Iterator 類別由復原容器支援例如集合中所有的 Iterator,在 Iterator 之間的減法可由隨機存取的容器所提供的隨機存取 Iterator 只支援,例如向量。

在 Visual C++ .NET Pocket PC, <hash_map><hash_set> 標頭檔 (Header File) 的成員不在 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 Class

標準樣板程式庫