[英]error: invalid use of incomplete type 'struct std::hash<>'
How do I create a hash function for key as pair of string and enum to be used in unordered_map? 如何为密钥创建哈希函数,以作为在unordered_map中使用的字符串和枚举对?
I have the following pair which is the key to an unordered map how do I create a hash so it can be used in unordered_map? 我有以下一对,这是无序映射的关键,如何创建哈希以便可以在unordered_map中使用?
enum Color {
Red,
Green
};
typedef std::pair <std::string, Color>
I tried the following but got compilation error 我尝试了以下操作,但出现编译错误
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
size_t hash( const PairType & pair ) {
return std::hash<std::string>()(pair.first) ^
std::hash<EnumClassHash>()(pair.second);
}
typedef std::unordered_map<PairType, std::string>,
std::function<decltype(hash)>> ColorMapType;
error: invalid use of incomplete type 'struct std::hash<EnumClassHash>'
std::hash<EnumClassHash>()(pairType.second);
The following isnt working either 以下都不起作用
size_t hash( const PairType & pairType ) {
return std::hash<std::string>()(pairType.first) ^
std::hash<Color>()(pairType.second);
}
typedef std::unordered_map<PairType, std::string, \
std::function<decltype(hash)>> ColorMapType;
ColorMapType colorMap(100, hash);
error: invalid use of incomplete type 'struct std::hash<Color>'
std::hash<Color>()(pair.second
); );
PS Pardon my formatting. PS请原谅我的格式。 I lost my gmail password and can only post from mobile app which is a tiny screen. 我丢失了Gmail密码,只能从一个很小的屏幕的移动应用发布信息。
struct EnumClassHash
, it is its own thing, not a specialization of std::hash
. 当声明struct EnumClassHash
,它是它自己的事情,而不是std::hash
的特殊化。 std::hash<EnumClassHash>()(pair.second)
, the template specialization std::hash<EnumClassHash>
simply doesn't exist. 当您编写std::hash<EnumClassHash>()(pair.second)
,模板专用化std::hash<EnumClassHash>
根本不存在。 You can either: 您可以:
struct EnumClassHash
, and use EnumClassHash
, or 声明struct EnumClassHash
,并使用EnumClassHash
,或者 std::hash
into std::hash<Color>
, and use std::hash<Color>
, or 将std::hash
专门化为std::hash<Color>
,然后使用std::hash<Color>
,或者 but never mix and match part of each method, which does not make any kind of sense. 但切勿混用每种方法的一部分,这没有任何意义。
Also, to use std::pair<std::string, Color>
as the key type of std::unordered_map
, neither std::hash<Color>
nor EnumClassHash
matters. 同样,要使用std::pair<std::string, Color>
作为std::unordered_map
的键类型, std::hash<Color>
或EnumClassHash
重要。 What matters is std::hash<std::pair<std::string, Color>>
or your own class that hashes std::pair<std::string, Color>
. 重要的是std::hash<std::pair<std::string, Color>>
或您自己的散列std::pair<std::string, Color>
。
Here is a recipe you can follow. 这是您可以遵循的食谱。
To use std::pair<std::string, Color>
as key type, you need to specialize std::hash<std::pair<std::string, Color>>
yourself. 要将std::pair<std::string, Color>
用作键类型,您需要自己专门设置std::hash<std::pair<std::string, Color>>
。
template <>
struct std::hash<std::pair<std::string, Color>>
{
std::size_t operator()(const std::pair<std::string, Color>& p) const {
return std::hash<std::string>()(p.first) ^ std::hash<Color>()(p.second);
}
};
But before that, you need to specialize std::hash<Color>
first since std::hash<std::pair<std::string, Color>>
uses it. 但是在此之前,由于std::hash<std::pair<std::string, Color>>
使用它,因此首先需要专门化std::hash<Color>
。
template <>
struct std::hash<Color>
{
std::size_t operator()(Color c) const {
return static_cast<size_t>(c);
}
};
Then, you can use something like std::unordered_map<std::pair<std::string, Color>, std::string>
. 然后,您可以使用std::unordered_map<std::pair<std::string, Color>, std::string>
。 No EnumClassHash
involved. 不涉及EnumClassHash
。
If you love EnumClassHash
very much, here is another recipe. 如果您非常喜欢EnumClassHash
,这是另一种方法。
You can keep EnumClassHash
as you like. 您可以根据需要保留EnumClassHash
。
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
This time, std::hash<Color>
does not exist. 这次, std::hash<Color>
不存在。 Don't use it. 不要使用它。 (Not to mention std::hash<EnumClassHash>
which does not make sense.) (更不用说std::hash<EnumClassHash>
。)
So, the class that hash the key type should use EnumClassHash
. 因此,对键类型进行哈希处理的类应使用EnumClassHash
。 (Once again, not std::hash<EnumClassHash>
!) (再次,不是std::hash<EnumClassHash>
!)
struct PairHash {
std::size_t operator()(const std::pair<std::string, Color>& p) const {
return std::hash<std::string>()(p.first) ^ EnumClassHash()(p.second);
}
};
Now, you can use std::unordered_map<std::pair<std::string, Color>, std::string, PairHash>
. 现在,您可以使用std::unordered_map<std::pair<std::string, Color>, std::string, PairHash>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.