【问题标题】:Is there an equivalent for stdext::hash_map in gccgcc 中是否有 stdext::hash_map 的等价物
【发布时间】:2015-04-28 18:53:29
【问题描述】:

我正在将一些代码从 Visual Studio 移植到 Mingw GCC,我遇到了这种类型

typedef stdext::hash_map<std::string, SubApplication*> SubApplMap;

现在据我了解,这不是标准类型。我遇到了这个thread aand this,它建议通过执行以下操作将其替换为 unordered_map

#include <unordered_map>
#define stdext std
#define hash_map unordered_map

结果我得到以下错误

 error: 'hash_multimap' is not a member of 'std'
 #define stdext std
                 ^

关于我可以用什么替换此容器的任何建议?

【问题讨论】:

  • 那些#defines 是个坏主意,你应该做这个工作并将stdext::hash_map 替换为std::unordered_map。希望接口足够相似,不会有太多的工作。至于错误,我的猜测是您的代码在其他地方使用了stdext::hash_multimap,而#define stdext std 则搞砸了。将该类型替换为std::unordered_multimap

标签: c++ visual-studio gcc dictionary


【解决方案1】:

代替

typedef stdext::hash_map<std::string, SubApplication*> SubApplMap;

我会一起摆脱hash_map

using SubApplMap = std::unordered_map<std::string, SubApplication*>;

而不是尝试使用预处理器宏玩游戏。

请注意,这需要 C++11。

【讨论】:

  • 是的,我启用了 C++11。我的 typedef 也在我的班级里。我应该评论 typedef 并将上面的语句而不是它(在类内)吗?
  • 是的,你应该这样做。其实不要随便注释掉,删掉这行:)
  • 我收到以下错误error: expected ';' before 'SubApplMap' using SubApplMap = std::unordered_map&lt;std::string, SubApplication*&gt; SubApplMap; 有什么建议吗?
  • 在分号之前的语句末尾有一个额外的SubApplMap,去掉它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 2013-06-21
  • 2014-01-09
  • 2012-02-18
相关资源
最近更新 更多