【问题标题】:c++ unordered_map compiling issue with g++c++ unordered_map 使用 g++ 编译问题
【发布时间】:2011-04-27 18:47:49
【问题描述】:

我在 Ubuntu 中使用 g++

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

我有这个代码

#include<unordered_map>
using namespace std;

bool ifunique(char *s){
  unordered_map<char,bool> h;
  if(s== NULL){
    return true;
  }
  while(*s){
    if(h.find(*s) != h.end()){
      return false;
    }
    h.insert(*s,true);
    s++;
  }
  return false;
}

当我使用

编译时
g++ mycode.cc

我有错误

 error: 'unordered_map' was not declared in this scope

我错过了什么吗?

【问题讨论】:

    标签: c++ hashtable unordered-map


    【解决方案1】:

    如果你不想在 C++0x 模式下编译,请将 include 和 using 指令更改为

    #include <tr1/unordered_map>
    using namespace std::tr1;
    

    应该工作

    【讨论】:

      【解决方案2】:

      在 GCC 4.4.x 中,您应该只需要#include &lt;unordered_map&gt;,并使用这一行进行编译:

      g++ -std=c++0x source.cxx

      更多关于C++0x support in GCC的信息。

      编辑您的问题

      插入时必须std::make_pair&lt;char, bool&gt;(*s, true)

      此外,您的代码只会插入一个字符(通过*s 取消引用)。您打算使用单个 char 作为键,还是要存储字符串?

      【讨论】:

      • 错误:没有匹配函数调用 'std::unordered_map, std::equal_to, std::allocator<: char bool> >::insert(char&, bool)'
      • @xlione:你能给我们看看代码吗?您似乎正在尝试在地图中插入引用类型。
      • g++ -std=c++11 现在是最新的了
      • 我检查了插入时不需要配对,通常它会起作用....就像 mp[i]=j;其中 mp 是 unordered_map 的对象。
      猜你喜欢
      • 2013-12-15
      • 2019-12-24
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多