使用g++编译C++的unordered_map时出现问题

8

我在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

我有什么遗漏吗?


2个回答

21

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

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

应该能够正常工作


15
在GCC 4.4.x中,您应该只需要包含 #include <unordered_map>,并使用以下命令进行编译:g++ -std=c++0x source.cxx
有关GCC中的C++0x支持的更多信息
关于您的问题的编辑:
在插入时,您必须执行std::make_pair<char, bool>(*s, true)
此外,您的代码只会插入单个字符(通过对*s 的解引用)。您是想要为键使用单个char,还是打算存储字符串?

错误:没有找到匹配的函数以调用'std :: unordered_map <char,bool,std :: hash <char>,std :: equal_to <char>,std :: allocator <std :: pair <const char,bool >> > :: insert(char&,bool)' - icn
@xlione:你能给我们展示一下代码吗?看起来你正在尝试将一个引用类型插入到你的映射中。 - wkl
1
g++ -std=c++11 是目前最新的版本。 - siddhusingh
我已经检查过了,在插入时不需要成对出现,通常它会正常工作……就像mp[i]=j;这样,其中mp是unordered_map对象。 - LoveToCode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接