• <small id='6HR1P'></small><noframes id='6HR1P'>

      <i id='6HR1P'><tr id='6HR1P'><dt id='6HR1P'><q id='6HR1P'><span id='6HR1P'><b id='6HR1P'><form id='6HR1P'><ins id='6HR1P'></ins><ul id='6HR1P'></ul><sub id='6HR1P'></sub></form><legend id='6HR1P'></legend><bdo id='6HR1P'><pre id='6HR1P'><center id='6HR1P'></center></pre></bdo></b><th id='6HR1P'></th></span></q></dt></tr></i><div id='6HR1P'><tfoot id='6HR1P'></tfoot><dl id='6HR1P'><fieldset id='6HR1P'></fieldset></dl></div>
    1. <legend id='6HR1P'><style id='6HR1P'><dir id='6HR1P'><q id='6HR1P'></q></dir></style></legend>
    2. <tfoot id='6HR1P'></tfoot>
      • <bdo id='6HR1P'></bdo><ul id='6HR1P'></ul>

        如何实现用于 std::unordered_map 的 CString 哈希函数?

        时间:2023-08-26

            <small id='TMl39'></small><noframes id='TMl39'>

            <i id='TMl39'><tr id='TMl39'><dt id='TMl39'><q id='TMl39'><span id='TMl39'><b id='TMl39'><form id='TMl39'><ins id='TMl39'></ins><ul id='TMl39'></ul><sub id='TMl39'></sub></form><legend id='TMl39'></legend><bdo id='TMl39'><pre id='TMl39'><center id='TMl39'></center></pre></bdo></b><th id='TMl39'></th></span></q></dt></tr></i><div id='TMl39'><tfoot id='TMl39'></tfoot><dl id='TMl39'><fieldset id='TMl39'></fieldset></dl></div>
          1. <tfoot id='TMl39'></tfoot>

              <tbody id='TMl39'></tbody>
          2. <legend id='TMl39'><style id='TMl39'><dir id='TMl39'><q id='TMl39'></q></dir></style></legend>
                <bdo id='TMl39'></bdo><ul id='TMl39'></ul>

                  本文介绍了如何实现用于 std::unordered_map 的 CString 哈希函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我要声明:

                  std::unordered_map<CString, CString> m_mapMyMap;
                  

                  但是当我构建时,我收到一个错误,告诉我标准 C++ 没有为 CString 提供哈希函数,而 CString 有 (LPCSTR) 运算符.

                  But when I build I got an error telling me that the standard C++ doesn't provide a hash function for CString, while CString have the (LPCSTR) operator.

                  如何正确实现 CString 的哈希函数?

                  How do I properly implement a hash function for CString?

                  推荐答案

                  基于 std::string 的 MS STL 实现,我创建了以下可用于 std 的方法::unordered_setstd::unordered_map:

                  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 <>
                      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)));
                          }
                      };
                  }
                  

                  或者更通用:

                  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)));
                          }
                      };
                  }
                  

                  这篇关于如何实现用于 std::unordered_map 的 CString 哈希函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:与 std::hash 意外冲突 下一篇:如何为来自其他库的类型专门化 std::hash

                  相关文章

                    <tfoot id='Iu2Jz'></tfoot>
                      <bdo id='Iu2Jz'></bdo><ul id='Iu2Jz'></ul>
                    1. <small id='Iu2Jz'></small><noframes id='Iu2Jz'>

                      <i id='Iu2Jz'><tr id='Iu2Jz'><dt id='Iu2Jz'><q id='Iu2Jz'><span id='Iu2Jz'><b id='Iu2Jz'><form id='Iu2Jz'><ins id='Iu2Jz'></ins><ul id='Iu2Jz'></ul><sub id='Iu2Jz'></sub></form><legend id='Iu2Jz'></legend><bdo id='Iu2Jz'><pre id='Iu2Jz'><center id='Iu2Jz'></center></pre></bdo></b><th id='Iu2Jz'></th></span></q></dt></tr></i><div id='Iu2Jz'><tfoot id='Iu2Jz'></tfoot><dl id='Iu2Jz'><fieldset id='Iu2Jz'></fieldset></dl></div>
                      <legend id='Iu2Jz'><style id='Iu2Jz'><dir id='Iu2Jz'><q id='Iu2Jz'></q></dir></style></legend>