如何将构造函数作为键存储在stdext :: hash_map中 [英] How to store structor as key in the stdext::hash_map

查看:66
本文介绍了如何将构造函数作为键存储在stdext :: hash_map中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "hash_map"

struct MyStruct
{
    MyStruct( const void* pInfo, UINT uNumber ) : m_pInfo( pInfo ),
                                                  m_uNumber( uNumber )
    {
    }
    friend bool operator< ( const MyStruct& stKey1_i, const MyStruct& stKey2_i )
    {
        if( stKey1_i.m_pInfo == stKey2_i.m_pInfo )
        {
            return stKey1_i.m_uNumber < stKey2_i.m_uNumber;
        }
        else
        {
            return stKey1_i.m_pInfo < stKey2_i.m_pInfo;
        }
    }
    const void* m_pInfo;
    UINT m_uNumber;
};

int _tmain(int argc, _TCHAR* argv[])
{
    typedef stdext::hash_map< const MyStruct, int > MyMap;
    MyMap map;
    MyStruct mystructObj1 = MyStruct( new char[20], 10 );
    map[ mystructObj1 ] = 500;
    return 0;
}


编译完上面的代码后,我得到以下错误消息
错误C2440:类型转换":无法从"const MyStruct"转换为"size_t"


After compiling the above code i got following error message
error C2440: ''type cast'' : cannot convert from ''const MyStruct'' to ''size_t''

How to solve this?

推荐答案

好吧,^ ],您将找到有关如何在自定义类/结构中使用hash_map的很好的解释. :)
Well, here[^] you will find a good explanation on how to use hash_map with your custom classes/structures. :)


将以下方法添加到MyStruct
add following method into MyStruct
operator size_t() const
{
    return sizeof( REV_DCM_CACHE_KEY );
}


这篇关于如何将构造函数作为键存储在stdext :: hash_map中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆