Comment implémenter une fonction de hachage CSsortingng à utiliser avec std :: unordered_map?

Je veux déclarer:

std::unordered_map m_mapMyMap; 

Mais lorsque je comstack, une erreur me dit que le C ++ standard ne fournit pas de fonction de hachage pour CSsortingng, alors que CSsortingng possède l’opérateur (LPCSTR).

Comment implémenter correctement une fonction de hachage pour CSsortingng?

Sur la base de l’implémentation MS STL pour std::ssortingng j’ai créé les méthodes suivantes pouvant être utilisées pour std::unordered_set et std::unordered_map :

 namespace std { template <> struct hash { // hash functor for CSsortingng size_t operator()(const CSsortingng& _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 { // hash functor for CSsortingngA size_t operator()(const CSsortingngA& _Keyval) const { // hash _Keyval to size_t value by pseudorandomizing transform return (_Hash_seq((const unsigned char*)(LPCSTR)_Keyval, _Keyval.GetLength() * sizeof(char))); } }; } 

Ou encore plus générique:

 namespace std { template struct hash> : public unary_function, size_t> { // hash functor for CSsortingngT typedef CSsortingngT _Kty; size_t operator()(const _Kty& _Keyval) const { // hash _Keyval to size_t value by pseudorandomizing transform return (_Hash_seq((const unsigned char*)(SsortingngTraits::PCXSTR)_Keyval, _Keyval.GetLength() * sizeof(BaseType))); } }; } 

std :: unordered_map utilise std :: hash <> qui n’utilise pas d’opérateur (LPCSTR) .

Vous devez redéfinir la fonction de hachage:

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

Mais pour de meilleures performances, utilisez std :: ssortingng à la place de CSsortingng pour key.