Based on the MS STL implementation for std::string
I created the following methods which can be used for std::unordered_set
and std::unordered_map
:
namespace std
{
template <typename BaseType, class StringTraits>
struct hash<CStringT<BaseType, StringTraits>>
{ // hash functor for CStringT<BaseType, StringTraits>
size_t operator()(const CStringT<BaseType, StringTraits>& _Keyval) const noexcept
{ // hash _Keyval to size_t value by pseudorandomizing transform
return (_Hash_array_representation(_Keyval.GetString(), _Keyval.GetLength()));
}
};
} // namespace std
// Usage:
std::unordered_set<CString> set1;
std::unordered_map<CString, CString> map1;
std::unordered_set<CStringA> set2;
std::unordered_map<CStringA, CStringA> map2;
Or use the following code if you want to specify a hasher (without putting your own code into the std
namespace):
struct CStringHasher
{
template <typename BaseType, class StringTraits>
size_t operator()(const CStringT<BaseType, StringTraits>& _Keyval) const noexcept
{ // hash _Keyval to size_t value by pseudorandomizing transform
return std::_Hash_array_representation(_Keyval.GetString(), _Keyval.GetLength());
}
};
// Usage:
std::unordered_set<CString, CStringHasher> set1;
std::unordered_map<CString, CString, CStringHasher> map1;
std::unordered_set<CStringA, CStringHasher> set2;
std::unordered_map<CStringA, CStringA, CStringHasher> map2;
Old answer which works up to C++11 (unary_function
and std::_HashSeq
were removed in C++17):
namespace std {
template <>
struct hash<CString>
{ // hash functor for CString
size_t operator()(const CString& _Keyval) const
{ // hash _Keyval to size_t value by pseudorandomizing transform
return (_Hash_seq((const unsigned char*)(LPCWSTR)_Keyval, _Keyval.GetLength() * sizeof(wchar_t)));
}
};
template <>
struct hash<CStringA>
{ // hash functor for CStringA
size_t operator()(const CStringA& _Keyval) const
{ // hash _Keyval to size_t value by pseudorandomizing transform
return (_Hash_seq((const unsigned char*)(LPCSTR)_Keyval, _Keyval.GetLength() * sizeof(char)));
}
};
}
Or even more generic:
namespace std {
template<typename BaseType, class StringTraits>
struct hash<CStringT<BaseType, StringTraits>> : public unary_function<CStringT<BaseType, StringTraits>, size_t>
{ // hash functor for CStringT<BaseType, StringTraits>
typedef CStringT<BaseType, StringTraits> _Kty;
size_t operator()(const _Kty& _Keyval) const
{ // hash _Keyval to size_t value by pseudorandomizing transform
return (_Hash_seq((const unsigned char*)(StringTraits::PCXSTR)_Keyval,
_Keyval.GetLength() * sizeof(BaseType)));
}
};
}