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

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

    1. <tfoot id='TQEex'></tfoot>

        为什么我不能用 std::unordered_map 替换 std::map

        时间:2024-05-11
      1. <i id='ar45u'><tr id='ar45u'><dt id='ar45u'><q id='ar45u'><span id='ar45u'><b id='ar45u'><form id='ar45u'><ins id='ar45u'></ins><ul id='ar45u'></ul><sub id='ar45u'></sub></form><legend id='ar45u'></legend><bdo id='ar45u'><pre id='ar45u'><center id='ar45u'></center></pre></bdo></b><th id='ar45u'></th></span></q></dt></tr></i><div id='ar45u'><tfoot id='ar45u'></tfoot><dl id='ar45u'><fieldset id='ar45u'></fieldset></dl></div>

              <tbody id='ar45u'></tbody>
              <bdo id='ar45u'></bdo><ul id='ar45u'></ul>

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

                  <legend id='ar45u'><style id='ar45u'><dir id='ar45u'><q id='ar45u'></q></dir></style></legend>
                  <tfoot id='ar45u'></tfoot>

                  本文介绍了为什么我不能用 std::unordered_map 替换 std::map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个问题可能有点粗略,因为我家里没有可用的代码,但我知道这件事否则会困扰我整个周末.

                  This question might be a bit sketchy because I do not have the code available at home, but I know this thing otherwise will bug me the whole weekend.

                  当我尝试将一些代码更新到 C++11 时,我开始用 std::unordered_map 替换一些 std::map.该代码仅使用 std::map::find() 来访问地图中的特定元素,因此我认为替换应该很容易.返回的迭代器被存储在一个 auto 类型的变量中(auto res = map.find( x ),所以类型应该检查得很好.但是当访问存储的元素时使用 res->second.do_stuff() 我得到一个编译器错误,告诉我 struct std::pair 没有成员 second. 现在这真的让我很困惑,但不幸的是我没有时间进一步调查.

                  When I tried to update some code to C++11 I began replacing some std::map with std::unordered_map. The code only used std::map::find() to access a specific element in the map, so I figured the replacement should be easy. The returned iterator was stored in an auto-typed variable (auto res = map.find( x ), so the typing should check out fine. However when accessing the stored element using res->second.do_stuff() I got a compiler error, telling me, that struct std::pair<char, B> does not have a member second. Now this really confused me, but unfortunately I did not have time to investigate further.

                  也许这是足够的信息,所以有人可以给我一个关于这个奇怪的编译器错误的提示.或者我的理解是 std::mapstd::unordered_map 应该有相同的接口,除了需要排序的部分,不正确?

                  Maybe this is enough information, so someone can give me a hint on this weird compiler error. Or is my understanding that std::map and std::unordered_map should have the same interface except for the parts which need an ordering, not correct?

                  编辑:

                  正如这里承诺的那样,对问题进行更多分析.很可能这将使某人现在可以更好地帮助我.正如我从评论中的提示中猜测的那样,这实际上并不是由我访问地图中元素的点引起的,而是由代码的其他部分引起的.我发现的原因是,我使用 X 类中的映射来存储指向 X 类其他元素的指针(一种树结构).然而,这似乎适用于 std::map 但不适用于 std::unordered_map.下面是一些展示问题的非常简单的代码:

                  As promised here some more analysis on the problem. Most likely this will allow someone to help me out better now. As I guessed from the hints in the comments, this was not really caused by the point where I accessed the elements in the map, but by some other part of the code. The reason I found was, that I used the map within Class X to store pointers to other elements of Class X (a kind of tree structure). However this seems to work for std::map but not for std::unordered_map. Here is some very simple code that exhibits the problem:

                  #include <stdint.h>
                  #include <unordered_map>
                  #include <map>
                  
                  class Test {
                    std::map<uint32_t, Test> m_map1; // Works
                    std::unordered_map<uint32_t, Test> m_map; // gives error: ‘std::pair<_T1, _T2>::second’ has incomplete type
                  };
                  
                  int main() {
                    return 1;
                  }
                  

                  std::map 有效 std::unordered_map 无效.任何想法为什么会这样,或者可以做些什么来让它与 std::unordered_map 一起工作?

                  std::map works std::unordered_map does not work. Any Ideas why this is the case, or what can be done to get it to work with a std::unordered_map?

                  推荐答案

                  我猜是因为 std::unordered_map 需要重新散列,因此复制元素,类型需要完整,而映射,只使用指针元素,不会出现这个问题.

                  I guess that because std::unordered_map needs to rehash, and therefore copy elements, the types need to be complete, whereas a map, only ever working with pointers to elements, will not exhibit that problem.

                  这里的解决方案是使用指向指针的无序映射:

                  The solution here is to have an unordered map to a pointer:

                  std::unordered_map<uint32_t, std::shared_ptr<Test> >. 
                  

                  这篇关于为什么我不能用 std::unordered_map 替换 std::map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Fortran 中的 STL 模拟 下一篇:const_iterator 和 iterator 有什么区别?

                  相关文章

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

                  1. <legend id='IywMm'><style id='IywMm'><dir id='IywMm'><q id='IywMm'></q></dir></style></legend>
                    • <bdo id='IywMm'></bdo><ul id='IywMm'></ul>
                    <tfoot id='IywMm'></tfoot>

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