Hash function for user defined class. How to make friends? :)(用户定义类的哈希函数.如何结交朋友?:))
问题描述
我有一个 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)
这篇关于用户定义类的哈希函数.如何结交朋友?:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用户定义类的哈希函数.如何结交朋友?:)
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
