case insensitive unordered_map<string, int>

c++, std, stl, string, unordered-map

Solution

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

Problem

How can I create a case-insensitive `unordered_map<string, int>`? Does overriding `key_equal` is sufficient or I also need to update `hasher`?

Original source