我正在尝试将 hash_map 用于我自定义的数据结构位置,这实际上是一个数组。当我尝试插入一些值对时,我遇到了错误。但我不知道为什么。
我在Windows7上编程,我使用的是VS 2010.以下是最低工作集。
#include <hash_map>
#include <iostream>
using namespace std;
#define HASH_NUM 8
struct Locations{
unsigned int loc[HASH_NUM];
};
//1. define the hash function
struct hash_Locations{
size_t operator()(const Locations & Loc) const
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
char* str = (char*)Loc.loc;
for (int i = 0; i < HASH_NUM * 4; i ++)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
};
//2. define less than function
struct less_than{
bool operator()(const Locations & x, const Locations & y) const
{
for(int i = 0; i < HASH_NUM; i ++){
if(x.loc[i] < y.loc[i])
return true;
}
return false;
}
};
int main(){
hash_map<Locations, unsigned int, hash_compare<hash_Locations, less_than> > location_map;
Locations x, y;
for(int i = 0; i < HASH_NUM; i ++){
x.loc[i] = i * 3;
y.loc[i] = i * 2;
}
//error
location_map.insert(pair<Locations, unsigned int>(x, 1));
location_map.insert(pair<Locations, unsigned int>(y, 2));
return 0;
}
编辑: 这是错误报告。
1>------ Build started: Project: hash_map, Configuration: Debug Win32 ------
1> hash_map.cpp
1>d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(854): error C2664: 'bool stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &,const _Kty &) const' : cannot convert parameter 1 from 'const Locations' to 'const hash_Locations &'
1> with
1> [
1> _Kty=hash_Locations,
1> _Pr=less_than
1> ]
1> Reason: cannot convert from 'const Locations' to 'const hash_Locations'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(849) : while compiling class template member function 'std::pair<_Ty1,_Ty2> std::_Hash<_Traits>::_Insert(const std::pair<const _Kty,_Ty> &,std::_List_iterator<_Mylist>)'
1> with
1> [
1> _Ty1=std::_List_iterator<std::_List_val<std::pair<const Locations,unsigned int>,std::allocator<std::pair<const Locations,unsigned int>>>>,
1> _Ty2=bool,
1> _Traits=std::_Hmap_traits<Locations,unsigned int,stdext::hash_compare<hash_Locations,less_than>,std::allocator<std::pair<const Locations,unsigned int>>,false>,
1> _Kty=Locations,
1> _Ty=unsigned int,
1> _Mylist=std::_List_val<std::pair<const Locations,unsigned int>,std::allocator<std::pair<const Locations,unsigned int>>>
1> ]
1> d:\program files (x86)\microsoft visual studio 10.0\vc\include\hash_map(91) : see reference to class template instantiation 'std::_Hash<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Hmap_traits<Locations,unsigned int,stdext::hash_compare<hash_Locations,less_than>,std::allocator<std::pair<const Locations,unsigned int>>,false>
1> ]
1> c:\documents and settings\daihuichen\桌面\hash_map\hash_map.cpp(42) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty,_Tr>' being compiled
1> with
1> [
1> _Kty=Locations,
1> _Ty=unsigned int,
1> _Tr=stdext::hash_compare<hash_Locations,less_than>
1> ]
1>d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(857): error C2664: 'bool stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &,const _Kty &) const' : cannot convert parameter 1 from 'const Locations' to 'const hash_Locations &'
1> with
1> [
1> _Kty=hash_Locations,
1> _Pr=less_than
1> ]
1> Reason: cannot convert from 'const Locations' to 'const hash_Locations'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:0)
以下内容可以解决您的问题,但不确定最佳解决方案。
struct hash_Locations{
// struct hash_Locations:public hash_compare<Locations, less_than>{
enum
{ // parameters for hash table
bucket_size = 4, // 0 < bucket_size
min_buckets = 8}; // min_buckets = 2 ^^ N, 0 < N
size_t operator()(const Locations & Loc) const
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
char* str = (char*)Loc.loc;
for (int i = 0; i < HASH_NUM * 4; i ++)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
bool operator()(const Locations & x, const Locations & y) const
{
for(int i = 0; i < HASH_NUM; i ++){
if(x.loc[i] < y.loc[i])
return true;
}
return false;
}
};
答案 1 :(得分:0)
我认为错误报告非常明确地说明了。
&#34;无法从&#39; const位置转换参数1&#39;到#const; hash_Locations&amp;&#39;&#34;
只需更改
bool operator()(const Locations & x, const Locations & y)
到
bool operator()(const Locations x, const Locations y)