Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create unordered_map for CString as key [duplicate]

I'm trying to create following unordered_map:

std::unordered_map<CString, CString, std::function<size_t(const CString &data)>> usetResponse(100, [](const CString &data)
    {
        return std::hash<std::string>()((LPCSTR)data);
    });

I provided hash function for CString, but compiler still returns errors:

error C2338: The C++ Standard doesn't provide a hash for this type. 

error C2664: 'std::unordered_map<CString,CString,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const
_Kty,_Ty>>>::unordered_map(std::initializer_list<std::pair<const _Kty,_Ty>>,unsigned int,const std::hash<_Kty> &,const _Keyeq &,const std::allocator<std::pair<const _Kty,_Ty>> &)' : cannot convert argument 1 from 'std::unordered_map<CString,CString,std::function<size_t (const CString &)>,std::equal_to<_Kty>,std::allocator<std::pair<const
_Kty,_Ty>>>' to 'const std::unordered_map<CString,CString,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const
_Kty,_Ty>>> &'

Please tell me what I'm doing wrong ?

like image 970
drewpol Avatar asked Oct 26 '25 11:10

drewpol


1 Answers

Something like this:

struct CStringHash
{
    size_t operator () (const CString &s) const
    {
        return hash<string>()(static_cast<LPCSTR>(s));
    }
};

Then declare the map like this:

unordered_map<CString, CString, CStringHash> map;
like image 55
Sid S Avatar answered Oct 28 '25 01:10

Sid S