为什么我不能用std :: unordered_map替换std :: map [英] Why can't I replace std::map with std::unordered_map

查看:912
本文介绍了为什么我不能用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 :: map std :: unordered_map 。代码只使用 std :: map :: find()来访问地图中的特定元素,所以我想替换应该很容易。返回的迭代器存储在 auto -typed变量( auto res = map.find(x)但是当使用 res-> second.do_stuff()访问存储的元素时,我得到一个编译器错误,告诉我, struct std :: pair< char,B>没有成员第二现在这真的让我困惑,但很遗憾,我没有时间进一步调查。

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 :: map code> std :: 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?

EDIT ::

EDIT:

正如我在这里对这个问题进行了更多的分析,很可能会让某人更好地帮助我,提示在注释中,这不是真正由我访问地图中的元素,而是由代码的其他部分的点。我发现的原因是,我使用类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 works 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需要rehash,因此复制元素,类型需要是完整的,而一个地图,只有使用指针元素,不会出现这个问题。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆