使用 hash_map,string> 失败,为什么?

发布于 2024-07-20 19:03:51 字数 4784 浏览 2 评论 0 原文

我已经实现了这段代码,但编译失败(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 也会发生同样的错误。

但用地图替换容器不会发生错误。
可以采取什么措施来纠正?
谢谢。

[已编辑]
错误信息:


------ 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 ==========

[已编辑]
完整的解决方案:


#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;
}

I have implemented this code, but fail at compilation (VC2008 Express Ed.) :
Now all code is here.


#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;
}

using unordered_map also occurs the same error too.

but replacing the container by a map error does not occur.
What can be done to correct?
Thank you.

[EDITED]
Error info :


------ 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]
Complete solution :


#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒冷纷飞旳雪 2024-07-27 19:03:52

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

对我来说听起来像是糟糕/没有设计......只要考虑一下计算所有这些哈希码的费用就可以了。

Even if it works, I smell fish... Under what circumstance do you want to use a vector-of-ints as a key to retrieve a string attribute from a hash-map?

Sounds like poor/no design to me... just think about the expense of calculating all those hashCodes for a start.

懒的傷心 2024-07-27 19:03:52

要比较键,<、==、> 应定义运算符。 这里的 find() 函数不知道如何比较键。

To compare the keys, <,==,> operators should be defined. The find() function here doesn't know how to compare the keys.

巾帼英雄 2024-07-27 19:03:51

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

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

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

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

First, make sure that the header that you include really exists on your installation.

Second, hash_map is in the stdext namespace, make sure you make this name visible via specifying it explicitly as stdext::hash_map, or via a using directive.

Third, where are the template arguments for your hash_map in your code? I only see hash_map and not hash_map<vector<int>,string>.

Fourth, I'm not sure if std::vector can be used as a hash key for a hash map just like this. You need to define a hash function for your key (via implementing a hash_compare function object and passing it to the hash_map as third template parameter). Anyway storing these vectors by value is hardly a good idea, instead you should probably consider using pointers to these vectors as keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文