为什么 hash 会将自身作为散列值返回?
我已经并排设置了一个散列和一个散列,字符串的一个按预期工作,而 int 的一个将自己作为散列值产生!
这是它应该如何工作的吗?
hash<int> h;
for( int i=0 ; i<15 ; ++i )
{
cout << "int hash for " << i << " is " << h(i) << endl;
}
hash<string> hs;
for( int i=0; i<15; ++i) {
stringstream ss;
ss << i;
cout << "string hash for " << i << " is " << hs(ss.str()) << endl;
}
结果是
+ g++-4.8 -std=c++11 -O2 -Wall -pedantic -Weffc++ -Wextra main.cpp
+ ./a.out
int hash for 0 is 0
int hash for 1 is 1
int hash for 2 is 2
int hash for 3 is 3
int hash for 4 is 4
int hash for 5 is 5
int hash for 6 is 6
int hash for 7 is 7
int hash for 8 is 8
int hash for 9 is 9
int hash for 10 is 10
int hash for 11 is 11
int hash for 12 is 12
int hash for 13 is 13
int hash for 14 is 14
string hash for 0 is 2297668033614959926
string hash for 1 is 10159970873491820195
string hash for 2 is 4551451650890805270
string hash for 3 is 8248777770799913213
string hash for 4 is 16215888864653804456
string hash for 5 is 7995238595416485409
string hash for 6 is 3835993668518112351
string hash for 7 is 905566266843721762
string hash for 8 is 17899137375351314596
string hash for 9 is 6623666755989985924
string hash for 10 is 2594088158685378223
string hash for 11 is 9717255990760309898
string hash for 12 is 11194622142508650503
string hash for 13 is 15638310844945205280
string hash for 14 is 1116181219906060362
你可以看到它运行在: http ://coliru.stacked-crooked.com/a/0c0e1536d19c533f