hash_map::value_type

备注

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

表示对象的类型在 hash_map 存储状态。

typedef pair<const Key, Type> value_type;

备注

因为一个关联容器中的密钥不能更改使用一个用非常数迭代器或引用,value_type 声明为 pair*<constkey_type,而不是 mapped_type>*pair<key_type, mapped_type>

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

示例

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

int main( ) 
{
   using namespace std;
   using namespace stdext;
   typedef pair <const int, int> cInt2Int;
   hash_map <int, int> hm1;
   hash_map <int, int> :: key_type key1;
   hash_map <int, int> :: mapped_type mapped1;
   hash_map <int, int> :: value_type value1;
   hash_map <int, int> :: iterator pIter;
   
   // value_type can be used to pass the correct type
   // explicitely to avoid implicit type conversion
   hm1.insert ( hash_map <int, int> :: value_type ( 1, 10 ) );

   // Compare other ways to insert objects into a hash_map
   hm1.insert ( cInt2Int ( 2, 20 ) );
   hm1[ 3 ] = 30;

   // Initializing key1 and mapped1
   key1 = ( hm1.begin( ) -> first );
   mapped1 = ( hm1.begin( ) -> second );

   cout << "The key of first element in the hash_map is "
        << key1 << "." << endl;

   cout << "The data value of first element in the hash_map is "
        << mapped1 << "." << endl;

   // The following line would cause an error because
   // the value_type is not assignable
   // value1 = cInt2Int ( 4, 40 );

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

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

要求

标头: <hash_map>

命名空间: stdext

请参见

参考

hash_map Class

标准模板库