'hash_map'未在此范围内使用g ++ 4.2.1声明

May*_*ank 5 c++ sgi

我正在尝试使用sgi hash_map.

#include <list>
#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <tr1/unordered_map>
#include <ext/hash_map>


using namespace std;
struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};


int main()
{
  hash_map<const char*, int, hash<const char*>, eqstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  cout << "september -> " << months["september"] << endl;
  cout << "april     -> " << months["april"] << endl;
  cout << "june      -> " << months["june"] << endl;
  cout << "november  -> " << months["november"] << endl;
} 
Run Code Online (Sandbox Code Playgroud)

在gcc4.2上我收到错误

listcheck.cc: In function 'int main()':
listcheck.cc:22: error: 'hash_map' was not declared in this scope
listcheck.cc:22: error: expected primary-expression before 'const'
listcheck.cc:22: error: expected `;' before 'const'
listcheck.cc:24: error: 'months' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

而相同的代码用3.4编译.

Ter*_*fey 8

使用<unordered_map>.hash_map是特定于供应商的扩展,由unordered_map替换.


sth*_*sth 5

include文件<ext/hash_map>引用GNU扩展哈希映射类,这在命名空间中声明__gnu_cxx.您可以显式限定模板名称或添加:

using namespace __gnu_cxx;
Run Code Online (Sandbox Code Playgroud)