2010-10-19 218 views
6

我在UbuntuC++ unordered_map编译问题与G ++

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

使用G ++我有这样的代码

#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 

我错过了什么吗?

回答

9

在GCC 4.4.x到,你应该只需要#include <unordered_map>,并与该行编译:约C++0x support in GCC

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

更多信息。

关于您的问题

编辑已插入时做std::make_pair<char, bool>(*s, true)

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

+0

错误:调用“STD没有匹配功能: :unordered_map ,std :: equal_to ,std :: allocator >>插入(char&,bool)' – icn 2010-10-19 23:47:06

+0

@xlione:Can你向我们展示代码?好像你正试图在你的地图中插入一个引用类型。 – birryree 2010-10-19 23:48:32

+0

更新,谢谢 – icn 2010-10-19 23:51:03

19

如果你不想来编译的C++ 0x模式,改变包括使用指令

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

应该工作

+1

它的工作原理!谢谢 – icn 2010-10-19 23:59:43