使用hash_map <vector <int>,string&gt;失败,为什么?</vector <int>

时间:2009-05-17 04:29:22

标签: c++ templates stl

我已实现此代码,但在编译时失败(VC2008 Express Ed。):
现在所有代码都在这里。


#include <stdio.h>
#include <vector>
#include <string>
#include <hash_map>

using namespace std;
using namespace stdext;

typedef vector type_key;

int main(int argc, char* argv[])
{
    type_key key;

    hash_map<type_key, string> map_segment;
    hash_map<type_key, string>::iterator iter_map_segment;

    iter_map_segment = map_segment.find(key);

    return 0;
}

使用unordered_map也会出现相同的错误。

但不会发生因地图错误而更换容器 可以做些什么来纠正?
谢谢。

[EDITED]
错误信息:


------ Build started: Project: map_key_test, Configuration: Debug Win32 ------
Compiling...
map_key_test.cpp
c:\program files\microsoft visual studio 9.0\vc\include\xhash(75) : error C2440: 'type cast' : cannot convert from 'const type_key' to 'size_t'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
        c:\program files\microsoft visual studio 9.0\vc\include\xhash(128) : see reference to function template instantiation 'size_t stdext::hash_value<_Kty>(const _Kty &)' being compiled
        with
        [
            _Kty=type_key
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\xhash(127) : while compiling class template member function 'size_t stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &) const'
        with
        [
            _Kty=type_key,
            _Pr=std::less<type_key>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\hash_map(78) : see reference to class template instantiation 'stdext::hash_compare<_Kty,_Pr>' being compiled
        with
        [
            _Kty=type_key,
            _Pr=std::less<type_key>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\xhash(191) : see reference to class template instantiation 'stdext::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
        with
        [
            _Kty=type_key,
            _Ty=std::string,
            _Tr=stdext::hash_compare<type_key,std::less<type_key>>,
            _Alloc=std::allocator<std::pair<const type_key,std::string>>,
            _Mfl=false
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\hash_map(88) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled
        with
        [
            _Traits=stdext::_Hmap_traits<type_key,std::string,stdext::hash_compare<type_key,std::less<type_key>>,std::allocator<std::pair<const type_key,std::string>>,false>
        ]
        c:\users\salamon\documents\visual studio 2008\projects\map_key_test\map_key_test.cpp(17) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty>' being compiled
        with
        [
            _Kty=type_key,
            _Ty=std::string
        ]
Build log was saved at "file://c:\Users\Salamon\Documents\Visual Studio 2008\Projects\map_key_test\Debug\BuildLog.htm"
map_key_test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

[EDITED]
完整的解决方案:


#include <stdio.h>
#include <vector>
#include <string>
#include <hash_map>
using namespace std;
using namespace stdext;

class Vector : public vector<int>
{
public:
    operator size_t() const { return (*this).size(); };
};
typedef Vector type_key;

struct less_vector
{
    bool operator()(const Vector & x, const Vector & y) const
    {
        if ( x != y )
            return true;

        return false;
    }
};

struct greater_vector
{
    bool operator()(const Vector & x, const Vector & y) const
    {
        if ( x == y )
            return true;

        return false;
    }
};

int main(int argc, char* argv[])
{
    Vector key;
    string str;

    hash_map<Vector, string, hash_compare <Vector, less_vector > > map_segment;
    hash_map<Vector, string, hash_compare <Vector, greater_vector > >::iterator iter_map_segment;

    //
    key.push_back(0);
    key.push_back(1);
    key.push_back(2);
    str = "012";
    map_segment [key] = str;
    //
    key.clear();
    key.push_back(1);
    key.push_back(0);
    key.push_back(2);
    str = "102";
    map_segment [key] = str;
    //
    key.clear();
    key.push_back(2);
    key.push_back(1);
    key.push_back(0);
    str = "210";
    map_segment [key] = str;
    //
    key.clear();
    key.push_back(1);
    key.push_back(0);
    key.push_back(2);

    iter_map_segment = map_segment.find(key);

    return 0;
}

3 个答案:

答案 0 :(得分:4)

首先,确保您安装的标头确实存在于您的安装中。

其次,hash_map位于stdext命名空间中,请确保通过明确指定为stdext::hash_map或通过using指令使此名称可见。

第三,代码中hash_map的模板参数在哪里?我只看到hash_map而不是hash_map<vector<int>,string>

第四,我不确定std :: vector是否可以像这样用作哈希映射的哈希键。您需要为键定义哈希函数(通过实现hash_compare函数对象并将其作为第三个模板参数传递给hash_map。无论如何,按值存储这些向量并不是一个好主意,相反,您应该考虑使用指向这些向量的指针作为键。

答案 1 :(得分:1)

即使它有效,我也会闻到鱼......在什么情况下你想使用矢量作为从哈希映射中检索字符串属性的键?

对我来说听起来很糟糕/没有设计......只要考虑一下开始计算所有这些hashCodes的费用。

答案 2 :(得分:0)

比较密钥,&lt;,==,&gt;应该定义运营商。这里的find()函数不知道如何比较键。