2

我正在尝试使用 unordered_set 来维护唯一的结构列表。我已经为结构 Name 定义了哈希函数,但是当我扩展 Name 结构以包含另一个结构成员 Address 时收到编译错误。我知道我需要指定必须如何对 Address 结构进行散列,但我似乎无法弄清楚在哪里/如何。

#include <unordered_set>
#include <string>

using namespace std;

struct Address
{
    int num;
};

struct Name
{
    string first;
    string second;
    Address address;
};


struct hashing_fn {

    size_t operator()(const Address &a ) const
    {
        return  hash<int>()(a.num);
    }

    size_t operator()(const Name &name ) const
    {
        return hash<string>()(name.first) ^ hash<string>()(name.second) ^ hash<Address>()(name.address);
    }
};

int main(int argc, char* argv[])
{
    unordered_set<Name,hashing_fn> ids;
    return 0;
}

更新

只是为了完成,这是修复:

template<>
struct hash<typename Address> {
    size_t operator()(const Address &a ) const
    {
        return  hash<int>()(a.num);
    }
};
4

1 回答 1

2

你从来没有定义过hash<Address>!相反,您必须使用自己的函数operator()(name.address)


简单的异或可能不是最好的解决方案。我强烈建议您复制 Boost'shash_combine()并将整个负载放入namespace std

template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
  std::hash<T> hasher;
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

namespace std
{
  template <> struct hash<Address>
  {
    inline size_t operator()(const Address & a) const
    {
      return hash<int>()(a.num);
    }
  };

  template <> struct hash<Name>
  {
    inline size_t operator()(const Name & a) const
    {
      size_t seed = 0;
      hash_combine(seed, name.first);
      hash_combine(seed, name.second);
      hash_combine(seed, name.address);
      return seed;
    }
  };
}

现在您可以直接使用这些类型(取决于实现相等比较器):std::unordered_set<Name> s;

于 2011-11-30T01:13:26.457 回答