【发布时间】:2015-08-30 20:01:11
【问题描述】:
我正在编写使用状态空间的类。我的问题是,我不知道在unordered_map 中使用多个值作为键的理想方式是什么。
它应该这样工作:
我使用值 创建状态对象,因此它将作为<1;0;8>:pointer_to_object 插入到哈希图中。我想要哈希图,因为我需要找到
尽可能快的对象。
是否可以将其中一个用作unordered_map 的键而不提前指定它们的大小?
编辑:
我尝试使用@the_mandrill 推荐的代码,如下所示:
template <class T>
typedef std::unordered_map<std::vector<T>, State<T>*, boost::hash<std::vector<T>> Map;
template <class T>
size_t hash_value(const std::vector<T>& vec)
{
std::size_t seed = 0;
for (const auto& val : vec) {
boost::hash_combine(seed, val);
}
return seed;
}
但是我收到了这个错误:
stateSpaceLib.cpp:79:83: error: template argument 3 is invalid
typedef std::unordered_map<std::vector<T>, State<T>*, boost::hash<std::vector<T>> Map;
^
stateSpaceLib.cpp:79:1: error: template declaration of ‘typedef’
typedef std::unordered_map<std::vector<T>, State<T>*, boost::hash<std::vector<T>> Map;
^
【问题讨论】:
-
这听起来正是
tuple的用途。但我不明白你最后一段是什么意思。 -
@AlanStokes 我已经重写了,希望现在更好一点。
-
Re: 语法错误 - 你需要一个额外的 '>':
boost::hash<std::vector<T>>>
标签: c++ vector unordered-map stdtuple state-space