简体   繁体   English

不区分大小写的unordered_map <string, int>

[英]case insensitive unordered_map<string, int>

How can I create a case-insensitive unordered_map<string, int> ? 如何创建不区分大小写的unordered_map<string, int>
Does overriding key_equal is sufficient or I also need to update hasher ? 覆盖key_equal是否足够,或者我还需要更新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. 还必须更新Hasher,因为默认哈希算法不会为仅在符号符号上不同的字符串产生相同的哈希码 -哈希码函数的基本属性,旨在与不区分大小写的字符串一起使用。

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: 这在ideone上显示了不同的值:

101669370
3305111549

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM