Visual Studio 2010を使用しており、C++11機能std::unordered_map :: hash_function()を使用して、キー文字列ごとに計算されるハッシュ値を出力しています。
ただし、内部的には、エントリはハッシュ値の昇順ではなくランダムな順序で保存されていることがわかりました。
以下の例では、Dadは最小のハッシュ値を計算し、my_family_height_mapの最初のエントリである必要がありますが、そうではありません。そして、最初の要素は、お父さんよりも高いハッシュ値を計算するsisペアです。
カム誰かが私がこの奇妙な行動を見ている理由を説明しますか?
// unorderedmap_usage.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <unordered_map>
#include <string>
#include <iostream>
int main()
{
typedef std::unordered_map<std::string, double> height_map;
height_map my_family_height_map;
my_family_height_map["Dad"] = 5.10;
my_family_height_map["Mom"] = 5.3;
my_family_height_map["Sis"] = 5.3;
my_family_height_map["Zach"] = 6.2;
height_map::const_iterator c_it;
// Print everything in the map
for(c_it = my_family_height_map.begin(); c_it!= my_family_height_map.end(); ++c_it)
{
std::cout << "Hash:" << hasher_func(c_it->first) << " Person is : " << c_it->first << " - height is " << c_it->second << "\n";
}
}
O / P:
ハッシュ:572570820人は:Sis-高さは5.3
ハッシュ:306541572人は:お父さん-高さは5.1
ハッシュ:1076885014人は:ママ-高さは5.3ハッシュ:
2613037907
人は:ザック-高さは6.2
続行するためのキー。。。