c ++ unordered_map 编译问题与g ++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3973659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:10:11  来源:igfitidea点击:

c++ unordered_map compiling issue with g++

c++hashtableunordered-map

提问by icn

I am using g++ in Ubuntu

我在 Ubuntu 中使用 g++

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

I have this code

我有这个代码

#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;
}

when I compile using

当我编译使用

g++ mycode.cc

I got error

我有错误

 error: 'unordered_map' was not declared in this scope

Am I missing something?

我错过了什么吗?

回答by Hugh

If you don't want to to compile in C++0x mode, changing the include and using directive to

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

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

should work

应该管用

回答by wkl

In GCC 4.4.x, you should only have to #include <unordered_map>, and compile with this line:

在 GCC 4.4.x 中,您应该只需要#include <unordered_map>使用以下行进行编译:

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

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

More information about C++0x support in GCC.

有关GCC 中 C++0x 支持的更多信息。

edit regarding your problem

编辑关于您的问题

You have to do std::make_pair<char, bool>(*s, true)when inserting.

std::make_pair<char, bool>(*s, true)插入时你必须这样做。

Also, your code would only insert a single character (the dereferencing via *s). Do you intend to use a single charfor a key, or did you mean to store strings?

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