hash_map 会保存每个键/值对,即使键相等
嘿伙计们,我正在使用 hash_map 将字符串相互关联,代码如下:
#include <string>
#include <iostream>
#include <hash_map>
using namespace std;
using namespace stdext;
struct StrCompare : public stdext::hash_compare<string> {
unsigned int operator()(const string str) const {
unsigned int hash = 0;
unsigned int len = str.length();
for (unsigned int i = 0; i < len; i++)
hash = 31 * hash + str[i];
return hash;
}
bool operator()(const string str1, const string str2) const {
return str1 == str2;
}
};
int main() {
hash_map<string, string, StrCompare> m;
m["asdf"] = "fe";
m["asdf"] = "asdf";
for (hash_map<string, string, StrCompare>::iterator i = m.begin(); i != m.end(); ++i)
cout << i->first << " " << i->second << endl;
system("PAUSE");
}
问题是输出是:
asdf asdf
asdf fe
Press any key to continue . . .
为什么会发生这种情况?我每次都尝试打印哈希值,但哈希值是相同的。
Hey guys, I am using a hash_map to relate strings to one another, with this code:
#include <string>
#include <iostream>
#include <hash_map>
using namespace std;
using namespace stdext;
struct StrCompare : public stdext::hash_compare<string> {
unsigned int operator()(const string str) const {
unsigned int hash = 0;
unsigned int len = str.length();
for (unsigned int i = 0; i < len; i++)
hash = 31 * hash + str[i];
return hash;
}
bool operator()(const string str1, const string str2) const {
return str1 == str2;
}
};
int main() {
hash_map<string, string, StrCompare> m;
m["asdf"] = "fe";
m["asdf"] = "asdf";
for (hash_map<string, string, StrCompare>::iterator i = m.begin(); i != m.end(); ++i)
cout << i->first << " " << i->second << endl;
system("PAUSE");
}
The problem is that the output is:
asdf asdf
asdf fe
Press any key to continue . . .
Why is this happening? I have tried printing the hashes each time, but the hash is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
hash_map
没有进入 C++0x 的原因之一是存在许多相互冲突的实现,而且缺乏可靠的规范。我会改用 C++0x 所接受的内容。
std::unordered_map
可能有一个又长又笨拙的名字,但语义是明确定义的;它不会存储重复的键(为此您可以使用 std::unordered_multimap 来代替)。One of the reasons
hash_map
didn't make it into C++0x is that there were a number of conflicting implementations, and little in the way of solid specification.I'd switch to what was accepted into C++0x instead.
std::unordered_map
may have a long, clumsy name, but the semantics are well-defined; it will not store duplicate keys (for that you'd usestd::unordered_multimap
instead).从示例的一些细节来看,您似乎正在使用 MSVC。 Microsoft 的
hash_map
使用比较函数与哈希函数结合使用,为哈希为相同值的键提供排序。这与 SGI 的hash_map
规范不同。具体来说(来自 http://msdn.microsoft.com/en-us/library/ 1s1byw77.aspx):因此,虽然将比较函数更改为 return
str1 != str2
似乎适用于您的测试,但实际上并不正确。在两个不同的字符串碰巧具有相同的哈希码的情况下,它会混淆hash_map
。From some of the details of your example, it looks like you're using MSVC. Microsoft's
hash_map
uses a comparison function that works in conjunction with the hash function to provide an ordering for keys that hash to the same value. This is different than SGI's specification ofhash_map
. Specifically (from http://msdn.microsoft.com/en-us/library/1s1byw77.aspx):So while changing the comparison function to return
str1 != str2
seems to work for your test, it's not really correct. It'll confuse thehash_map
in the situation where two different strings happen to have the same hashcode.哦,显然我使用了
错误的功能。而不是
应该是
Oh, apparently I'm using the
function wrong. Instead of
it should be