【发布时间】:2016-07-28 04:42:11
【问题描述】:
原题有点长,这里简化一下。
我需要创建一组带有相关整数的字符串,比如说一个训练组。然后我需要创建许多培训组。我想在一个容器中管理所有培训组。所以我决定使用 boost::unordered_map,键是 std::unordered_set。因为,BOOST 具有标准 C++ 容器的哈希值。
简化代码如下:
#include <string>
#include <unordered_set>
#include <utility>
#include<boost/unordered_map.hpp>
using namespace std;
int main()
{
boost::unordered_map< unordered_set<string>, int> training_groups;
pair<unordered_set<string>, int> a_training_group;
training_groups.insert(a_training_group);
return 0;
}
但是,代码没有编译成功。有许多神秘的警告和错误。错误如下:
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(30): error C2440: 'type cast' : cannot convert from 'const std::unordered_set<std::string,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<_Kty>>' to 'size_t'
1> with
1> [
1> _Kty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> C:\Program Files\boost\boost_1_59_0\boost/functional/hash/extensions.hpp(262) : see reference to function template instantiation 'size_t stdext::hash_value<T>(const _Kty &)' being compiled
1> with
1> [
1> T=std::unordered_set<std::string,std::hash<std::string>,std::equal_to<std::string>,std::allocator<std::string>>
1> , _Kty=std::unordered_set<std::string,std::hash<std::string>,std::equal_to<std::string>,std::allocator<std::string>>
1> ]
我不知道这个错误的根源在哪里以及如何解决它。如果编译器无法归档 unordered_set 的哈希函数,则错误信息将包含“Hash”或“Key”等字样。但是,它只是说明了类型转换,看起来类似于散列函数。所以,我感到很困惑。
谁能给点建议。我在 Windows 8 上使用 Visual Studio 2013。
PS:当我将 Key unordered_set<string> 更改为 set<string> 或 vector<string> 时,程序编译成功。但是还是不知道是什么原因,如果我下定决心要用unordered_set<string>作为key,也不知道怎么解决。
【问题讨论】:
-
为什么你认为集合必须是地图的关键?
-
对于我真正的问题,训练组的第二部分实际上是另一个自定义类型的unordered_set。在这篇文章中,我使用整数来简化问题。所以,相比于自定义类型的 unordered_set,字符串的 unordered_set 更适合作为 Key。另外,我可以将 Key unordered_set 更改为 set 或 vector。但是,unordered_set 对于以后的操作更方便、更快捷。
-
@JohnSmith:使用
unordered_set作为键确实不是很快,因为比较谓词真的很慢。它可以是集合大小的二次方(正是因为集合是无序的)。
标签: c++ boost hash containers unordered