2013-08-04 40 views

回答

2

由於默認散列算法does not produce identical hash code for strings that differ only in the case of their symbols - 散列碼函數的一個基本屬性,用於處理大小寫不敏感的字符串,因此還需要更新散列函數。

std::string s1 = "Hello"; 
std::string s2 = "hello"; 
std::hash<std::string> hash_fn; 

size_t hash1 = hash_fn(s1); 
size_t hash2 = hash_fn(s2); 

std::cout << hash1 << '\n'; 
std::cout << hash2 << '\n'; 

這顯示了不同價值觀上ideone:

101669370 
3305111549 
+0

你的答案是正確的一般,但我們在這裏談論'的std ::哈希'函數。所以你的回答可能是錯誤的,這取決於'std :: hash '適用於字符串(我不知道)! – MBZ

+0

@MBZ改變'string'的std :: hash來忽略字符情況是沒有意義的:這個函數在CPU使用方面會更加昂貴,並且也會導致更多的衝突。 – dasblinkenlight

+0

你可以使所有的字符串小寫,然後你將它們散列掉 – aaronman