ttt

getttyent

GCCのhash_map

2009-08-06 23:59:00 | デジタル・インターネット

久しぶりにC++でコードを書いたら、かなり忘れていて、勘を取り戻すまで1時間くらいかかりました

さて、hash_mapを使おうと思ったんですが、コンパイルできない。今のFreeBSD7.2付属のGCCのバージョンは4.2.1ですが、昔C++を使ってたのは、バージョン3系だったかなぁ。

% g++ -v
Using built-in specs.
Target: i386-undermydesk-freebsd
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 4.2.1 20070719  [FreeBSD]

なにやらいろいろ変わっているようで、ヘッダファイルhash_mapの場所も違うし、namespaceも変わってる。こんな風に書くことになるみたいです。

% cat hash.cc
#include <iostream>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

int
main( int argc, char* argv[] )
{
  hash_map<int,string> hm;

  hm[0] = "Hello";
  hm[1] = "world";

  cout << hm[0] << ' ' << hm[1] << endl;

  return 0;
}

本当は、連想配列みたいなことをやりたくて、具体的には、hash_map<string,int>を使いたかったんです。

  hash_map<string,int> hm;

  hm["Hello"] = 1;

よくわかんないけど、GCCでは、これはできないんですね・・・なんだか、このhash_mapって存在意義まるでなし!って気がしましたが。

あと、それから、GCC 4.3.4でコンパイルしてみようとすると、deprecatedと言われてしまいました。

% g++43 hash.cc
In file included from /usr/local/lib/gcc43/include/c++/ext/hash_map:64,
                 from hash.cc:2:
/usr/local/lib/gcc43/include/c++//backward/backward_warning.h:33:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.

というわけで、hash_mapを使わないコードを書きましたとさ。

最初、

using namespace __gnu_cxx;

を書くと、__gnu_cxx is not namespace というようなエラーメッセージが出てて、どうしていいのかわからず、

__gnu_cxx::hash_map<int,string> hm;

と書いてたんですが、今さっき、何度か試しているうちに、最初の書き方でも大丈夫になっちゃたなあ。暑くて脳が働いていないのかも。