我正在尝试创建以下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);
});
我为CString提供了哈希函数,但编译器仍然返回错误:
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>>> &'
请告诉我我做错了什么?
答案 0 :(得分:2)
这样的事情:
struct CStringHash
{
size_t operator () (const CString &s) const
{
return hash<string>()(static_cast<LPCSTR>(s));
}
};
然后像这样声明地图:
unordered_map<CString, CString, CStringHash> map;