在unordered_set中使用string *作为键 [英] Using string* as a key in an unordered_set
本文介绍了在unordered_set中使用string *作为键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在unordered_list中使用字符串*作为键。我不想要散列指针本身,但它指向的字符串。
I would like to put use a string* as a key in an unordered_list. I do not want the hash the pointer itself but the string it points to.
我知道我需要创建一个这样的结构:
I understand I need to create a struct like this:
struct myhash{
size_t operator()(const string * str){
return hash(*str);
}
}
并将其作为哈希发送到地图模板,但我不知道如何。
and send it as a a hasher to the map template, but i am not sure how.
推荐答案
这基本上是。然后将其作为第三个模板参数提供给 unordered_map
类型(我假设它是C ++ 0x)。我将其概括为它可以在任何情况下使用,而不是只是 string
:
That's basically it. You then provide it as the third template parameter to the unordered_map
type (Which I will assume to be the C++0x one). I would generalize it so it's usable in any situation, rather than just string
:
struct dereference_hash
{
template <typename T>
std::size_t operator()(const T* pX)
{
return std::hash<T>()(*pX);
}
};
typedef std::unordered_map<std::string*, int, dereference_hash> map_type;
这篇关于在unordered_set中使用string *作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文