hash_map 会保存每个键/值对,即使键相等

发布于 2024-10-03 01:39:10 字数 980 浏览 1 评论 0原文

嘿伙计们,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日情怀 2024-10-10 01:39:10

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 use std::unordered_multimap instead).

束缚m 2024-10-10 01:39:10

从示例的一些细节来看,您似乎正在使用 MSVC。 Microsoft 的 hash_map 使用比较函数与哈希函数结合使用,为哈希为相同值的键提供排序。这与 SGI 的 hash_map 规范不同。具体来说(来自 http://msdn.microsoft.com/en-us/library/ 1s1byw77.aspx):

对于序列中位于 _Key2 之前且具有相同哈希值(哈希函数返回的值)的 Key 类型的任何值 _Key1 ),hash_comp(_Key2, _Key1) 为 false。该函数必须对 Key 类型的值进行全排序。 hash_compare 提供的函数返回 comp(_Key2, _Key1),其中 comp 是一个 Traits 类型的存储对象,您可以在构造时指定对象hash_comp。对于默认的 Traits 参数类型 less,排序键的值永远不会减少。

因此,虽然将比较函数更改为 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 of hash_map. Specifically (from http://msdn.microsoft.com/en-us/library/1s1byw77.aspx):

For any value _Key1 of type Key that precedes _Key2 in the sequence and has the same hash value (value returned by the hash function), hash_comp(_Key2, _Key1) is false. The function must impose a total ordering on values of type Key. The function supplied by hash_compare returns comp(_Key2, _Key1), where comp is a stored object of type Traits that you can specify when you construct the object hash_comp. For the default Traits parameter type less<Key>, sort keys never decrease in value.

So while changing the comparison function to return str1 != str2 seems to work for your test, it's not really correct. It'll confuse the hash_map in the situation where two different strings happen to have the same hashcode.

暗地喜欢 2024-10-10 01:39:10

哦,显然我使用了

bool operator()(const string str1, const string str2) const

错误的功能。而不是

bool operator()(const string str1, const string str2) const {
    return str1 == str2;
}

应该是

bool operator()(const string str1, const string str2) const {
    return str1 != str2;
}

Oh, apparently I'm using the

bool operator()(const string str1, const string str2) const

function wrong. Instead of

bool operator()(const string str1, const string str2) const {
    return str1 == str2;
}

it should be

bool operator()(const string str1, const string str2) const {
    return str1 != str2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文