How can I create a case-insensitive unordered_map<string, int>
?
Does overriding key_equal
is sufficient or I also need to update hasher
?
Hasher needs to be updated as well, because the default hash algorithm does not produce identical hash code for strings that differ only in the case of their symbols - an essential property of hash code function intended to work with case-insensitive strings.
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';
This shows different values on ideone:
101669370
3305111549
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.