7

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

我错过了什么吗?

4

2 回答 2

21

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

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

应该管用

于 2010-10-19T23:54:07.903 回答
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键用作键,还是要存储字符串?

于 2010-10-19T23:41:15.517 回答