How do I implement a CString hash function for use with std::unordered_map?

后端 未结 2 1655

I want to declare :

std::unordered_map m_mapMyMap;

But when I build I got an error telling me that the standard C++

2条回答
  •  爱一瞬间的悲伤
    2020-12-11 19:38

    std::unordered_map use std::hash<> that does not use (LPCSTR) operator.

    You need to redefine hash function:

    template class MyHash;
    
    template<>
    class MyHash {
    public:
        size_t operator()(const CString &s) const
        {
            return std::hash()( (LPCSTR)s );
        }
    };
    
    std::unordered_map m_mapMyMap;
    

    But for better performance use std::string instead CString for key.

提交回复
热议问题