2010-04-28 55 views
0

當gore添加unoreded_map支持時?gcc支持boost :: unordered_map

我使用的是RHEL 5.3附帶的gcc 4.1.1。 它看起來像unoreded_map丟失。有沒有辦法手動添加它?

回答

8

gcc沒有boost::unordered_map - 它是Boost的一部分。它有std::tr1::unordered_map。至少從4.0開始包含它。

要使用std::tr1::unordered_map,包括這個頭:

#include <tr1/unordered_map> 

因爲後者從前者創建的boost::unordered_mapstd::tr1::unordered_map接口應該是相似的。

+0

如果你的C++ 0x支持啓用,你可以的#include kgriffs 2011-10-20 16:51:07

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