0

我正在尝试将hash_map用于我的自定义数据结构Locations,它实际上是一个数组。当我尝试插入一些值对时,出现错误。但我不知道为什么。

我在 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 ==========
4

2 回答 2

0

我认为错误报告非常清楚地说明了这一点。

“无法将参数 1 从 'const Locations' 转换为 'const hash_Locations &'”

只是改变

bool operator()(const Locations & x, const Locations & y)

进入

bool operator()(const Locations x, const Locations  y)
于 2014-11-14T05:48:56.800 回答
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;
}

};

于 2013-06-27T17:59:30.647 回答