用户定义类的哈希函数.如何结交朋友?:)

2021-12-27 00:00:00 hash c++ c++11 unordered-map

我有一个 C 类,它有一个 string* ps 私有数据成员.
现在,我想要一个 unordered_map,我需要一个自定义哈希函数.

根据 c++ 参考,我可以这样做

命名空间标准{模板<>类哈希{上市:size_t operator()(const C &c) const{返回 std::hash()(*c.ps);}};}

问题是我似乎无法让 operator()C 成为朋友,以便我可以访问 ps.

我已经试过了:

C 类;模板<>类 std::hash<C>;C类{//...朋友 std::hash<C>::operator ()(const C&) const;//错误:类型不完整};//定义哈希这里.

但它说不完整的类型......在嵌套名称说明符......

我也无法扭转定义,因为如果稍后定义类 C,hash<C> 无法知道 ps.>

我在这里做错了什么?如何在不公开 ps 的情况下修复这种情况?

解决方案

试试这个:

C 类;命名空间标准{模板<>结构散列<C>{上市:size_t operator()(const C &c) const;//还没有定义};}C类{//...朋友 size_t std::hash<C>::operator ()(const C&) const;};命名空间标准{模板<>size_t hash<C>::operator()(const C &c) const {返回 std::hash()(*c.ps);}}

或者这个:

C 类;模板<>struct std::hash<C>;C类{朋友结构 std::hash<C>;//为类添加友元,而不是成员函数};

(我没有编译所以可能有语法错误)

I have a class C, which has a string* ps private data member.
Now, I'd like to have an unordered_map<C, int> for which I need a custom hash function.

According to the c++ reference, I can do that like

namespace std {
  template<>
  class hash<C> {
  public:
    size_t operator()(const C &c) const
    {
      return std::hash<std::string>()(*c.ps);
    }
  };
}

The problem is that I can't seem to make operator() and C friends so that I could access ps.

I have tried this:

class C;
template<>
class std::hash<C>;
class C{
  //...
  friend std::hash<C>::operator ()(const C&) const; // error: Incomplete type 
};
// define hash<C> here.

but it says that Incomplete type ... in nested name specifier ...

I can't turn around the definitions either, because if class C is defined later, the hash<C> has no way to know about ps.

What am I doing wrong here? How can this situation be fixed without making ps public?

解决方案

Try this:

class C;
namespace std {
  template<>
  struct hash<C> {
  public:
    size_t operator()(const C &c) const; // don't define yet
  };
}
class C{
  //...
  friend size_t std::hash<C>::operator ()(const C&) const;
};
namespace std {
  template<>
  size_t hash<C>::operator()(const C &c) const {
    return std::hash<std::string>()(*c.ps);
  }
}

Or this:

class C;
template<>
struct std::hash<C>;
class C{
  friend struct std::hash<C>; // friend the class, not the member function
};

(I haven't compiled so there might be a syntax error)

相关文章