0

何时将 unoreded_map 支持添加到 gcc?

我正在使用 RHEL 5.3 附带的 gcc 4.1.1。看起来 unoreded_map 丢失了。有没有办法手动添加?

4

2 回答 2

8

gcc 没有boost::unordered_map——它是Boost的一部分。它有std::tr1::unordered_map. 至少从 4.0 开始包含它。

要使用std::tr1::unordered_map,请包含此标头:

#include <tr1/unordered_map>

boost::unordered_map和的接口std::tr1::unordered_map应该相似,因为后者是从前者创建的。

于 2010-04-28T19:33:31.413 回答
2

在较旧的 gcc 版本上,您还可以使用 hash_map,这可能“足够好”。

#include <ext/hash_map> // Gnu gcc specific!
...

// allow the gnu hash_map to work on std::string
namespace __gnu_cxx {
   template<> struct hash< std::string > {
      size_t operator()(const std::string& s) const {
         return hash< const char* >()( s.c_str() );
      }
   }; /* gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html */
}

// this is what we would love to have:
typedef __gnu_cxx::hash_map<std::string, int> Hash;
....

然后

Hash hash;
string this_string;

...

hash[ this_string ]++;

...

我确实经常使用它并取得了成功。

问候

rbo

于 2010-04-28T19:42:11.870 回答