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

发布于 2024-09-28 13:01:16 字数 575 浏览 7 评论 0原文

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

我是否遗漏了什么?

I am using g++ in Ubuntu

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-10-05 13:01:16

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

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

应该可以

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

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

should work

紫轩蝶泪 2024-10-05 13:01:16

在 GCC 4.4.x 中,您只需#include,并使用以下行进行编译:

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

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

编辑您的问题

您必须在插入时执行std::make_pair(*s, true)

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

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

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

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

edit regarding your problem

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文