我試圖用stdext::hash_value()
在VS2010一些散列算法,實現了這一點:有趣stdext ::哈希值()實現
#include <iostream>
#include <xhash>
using namespace std;
int main()
{
#ifdef _WIN64
std::cout << "x64" << std::endl;
#else
std::cout << "x32" << std::endl;
#endif
std::cout << stdext::hash_value(345.34533) << std::endl;
std::cout << stdext::hash_value(345.566) << std::endl;
return 0;
}
// Output is:
// x64
//3735928758
//3735928758
我想,有相同的整數,但不同的小數部分的雙變量的其他一些夫婦。像1.234 vs 1.568一樣。哈希值總是相同的。所以,我接過來一看在hash_value()
源,看到
#define _HASH_SEED (size_t)0xdeadbeef
template<class _Kty> inline
size_t hash_value(const _Kty& _Keyval)
{ // hash _Keyval to size_t value one-to-one
return ((size_t)_Keyval^_HASH_SEED);
}
_KeyVal
被轉換爲size_t
不道理給我。該函數簡單地忽略了double的小數部分。這個實現背後的邏輯是什麼?它是一個錯誤或功能?
我覺得這個函數實際上是從Visual Studio 6開始的。這可以解釋很多。 – 2011-04-01 17:06:28
@dauphic:我不知道Visual Studio 6,但是它在''頭部的'stdext'命名空間中。那些舊的? –
2011-04-01 17:13:45
是的,'stdext'的內容最初位於'std'命名空間,但是它們被移動到'stdext',因爲它們不是標準庫的一部分(因此不應該在'std'命名空間中)。我不確定哪個版本移動了它們,但是它們在Visual Studio 7中是'stdext',因此它們必須追溯到1998年或更早發佈的Visual Studio 6。 – 2011-04-01 22:55:27