简体   繁体   English

C ++:不区分大小写的“ Boost:unordered_map”不起作用吗?

[英]C++: Case insensitive “Boost:unordered_map” does not work?

I tried to switch from std::unordered_map (VS2010) to boost::unordered_map (version 1.48) and surprisingly, some important test cases failed in my project. 我试图从std :: unordered_map(VS2010)切换到boost :: unordered_map(版本1.48),并且令人惊讶的是,一些重要的测试用例在我的项目中失败了。 I tracked down the cause and come to the conclusion that boost::unordered_map does not honor my case insensitive equality provider: 我找到了原因,得出了boost :: unordered_map不尊重我的不区分大小写的相等提供程序的结论:

struct StringEqualityCaseInsensitive : public std::equal_to<String>
{
    bool operator ()(const String& a, const String& b) const { return boost::iequals<String, String>(a, b); }
};

boost::unordered_map<string, int, boost::hash<string>, StringEqualityCaseInsensitive> map;

Now I just add some uppercase elements and search for their lowercase counterparts (using the find() member method). 现在,我仅添加一些大写元素并搜索它们的小写副本(使用find()成员方法)。 If I use the std::unordered_map it works just fine and with boost it doesn't. 如果我使用std :: unordered_map,它就可以正常工作,而使用boost则不能。 The cruel thing is that if I look for uppercase elements, the equality comparator gets invoked and when I look for lowercase, it doesn't get invoked... 残酷的是,如果我查找大写元素,则调用相等比较器,而当我查找小写时,它不会被调用...

Anyone's got a clue, why is this? 有人知道了,这为什么呢? (Not sure if this is important but I am using the Intel Compiler 12.1 with C++0x support enabled) (不确定这是否重要,但是我使用的是启用了C ++ 0x支持的Intel Compiler 12.1)

EDIT: Damn, now it dawns on me. 编辑:该死的,现在它突然降临在我身上。 Maybe I need to also adjust the hash class to return the same value independently of lower/upper-case. 也许我还需要调整哈希类以独立于小写/大写返回相同的值。 But still its strange that they have different behaviour?! 但是奇怪的是,他们有不同的行为?!

Thanks! 谢谢!

I doubt it will work in either boost::unordered_map or std::unordered_map , because your hash function is defined wrongly. 我怀疑它是否可以在boost::unordered_mapstd::unordered_map ,因为您的哈希函数定义错误。 The default boost::hash<string> is not case insensitive, meaning one of the fundamental assumption of hash tables 默认的boost::hash<string> 区分大小写,这意味着哈希表的基本假设之一

a == b   =>   hash(a) == hash(b)

is broken (ie HELLO and hello could generate different hashes). 已损坏(即HELLOhello可能生成不同的哈希值)。 The two maps give different result is just an implementation detail. 这两个映射给出不同的结果只是实现细节。

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

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