Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case insensitive unordered_map<string, int>

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

like image 664
MBZ Avatar asked Aug 04 '13 00:08

MBZ


1 Answers

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
like image 169
Sergey Kalinichenko Avatar answered Oct 30 '22 13:10

Sergey Kalinichenko