If you have tried to use some STL containers with GCC, such as hash_map:
// error: hash_map: No such file or directory #includeint main () { // error: ‘hash_map’ is not a member of ‘std’ std:: hash_map< int , int > hm; return 0 ; }
Then you have realized that the code above does not compile. That's because on GCC, hash_map is not regarded as a standard container, but rather as a extension included in the
__gnu_cxx namespace
. In order to use hash_map and other extended containers with a minimum impact in your code (which is very important if it's intended to be cross-platform), you can use the following solution:
#ifdef __GNUC__ #include#else #include namespace std { using namespace __gnu_cxx ; } int main () { std:: hash_map< int , int > hm; return 0 ; }#endif
Hope that helps.
----------------------------------------------------------------------------------------------------------------------------------
【2】
http://www.velocityreviews.com/forums/t280027-why-wont-this-hashmap-compile.html
Hi,
I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It
will compile if I use
I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It
will compile if I use
Mark
-----------------------------------------------------------------------------------------------------------------------------------
【3】
Re: gcc-3.0.2 + hash_map compilation problems
- From: Stefan Olsson
- To: Matthew Sundling
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Mon, 08 Apr 2002 23:42:45 +0200
- Subject: Re: gcc-3.0.2 + hash_map
compilation problems - Organization: Noname4us
- References: <[email protected]>
Hi!
I have had the same issue and solved it by declaring:
#include
#include
namespace __gnu_cxx
{
template<> struct hash< std::string >
{
size_t operator()( const std::string& x ) const
{
return hash< const char* >()( x.c_str() );
}
};
}
I guess that the reason for not having this declared in the std::string
is that the hash* are extensions to the standard. Maybe something like
the above could be added into the codebase somewhere else?
Hope that helps!
/Stefan
Matthew Sundling wrote:
>I have been trying to compile an extremely simple program that
>uses a hash_map. I can't quit figure out what the
>errors are telling to supply to the STL container to satisfy it.
>
>Thanks for any insight you might have:
>
>[---------------------------------------------------------------]
>My program:
>
>#include
>#include
>#include
>
>int main () {
> std::hash_map str_hash;
> str_hash["text"] = 1;
> std::cout << str_hash["test"] << std::endl;
> return 0;
>}
>
>
>[---------------------------------------------------------------]
>My gcc output:
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h: In member function
>`size_t std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const
>[with _Val = std::pair,
>_Key = std::string,
>_HashFcn = std::hash,
>_ExtractKey = std::_Select1st
>
>>,
>>
>_EqualKey = std::equal_to,
>_Alloc = std::allocator]':
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:512: instantiated
>from `size_t std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::_M_bkt_num(const _Val&, unsigned int) const
>[with _Val = std::pair,
>_Key = std::string, _HashFcn = std::hash,
>_ExtractKey = std::_Select1st
>
>>,
>>
>_EqualKey = std::equal_to,
>_Alloc = std::allocator]'
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:856: instantiated
>from `void std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::resize(unsigned int)
>[with _Val = std::pair,
>_Key = std::string,
>_HashFcn = std::hash,
>_ExtractKey = std::_Select1st
>
>>,
>>
>_EqualKey = std::equal_to,
>_Alloc = std::allocator]'
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:669: instantiated
>from `_Val& std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::find_or_insert(const _Val&)
>[with _Val = std::pair,
>_Key = std::string,
>_HashFcn = std::hash,
>_ExtractKey = std::_Select1st
>
>>,
>>
>_EqualKey = std::equal_to,
>_Alloc = std::allocator]'
>
>/s/unsup/include/g++-v3/ext/hash_map:173: instantiated from
>`_Tp& std::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
>_Alloc>::operator[](typename std::hashtable_Tp>, _Key, _HashFcn, std::_Select1st
>
>>, _EqualKey, _Alloc>::key_type&)
>>
>[with _Key = std::string,
>_Tp = int,
>_HashFcn = std::hash,
>_EqualKey = std::equal_to,
>_Alloc = std::allocator]'
>
>src/junk.cpp:7: instantiated from here
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:507: no match for
>call to `(const std::hash) (const std::basic_string std::char_traits, std::allocator >&)'
>
>
>Thanks!
>Matt
>
>------------------
>Matthew Sundling
>[email protected]
>
>
>
看了上面三个资料后,我想你可以像使用一个map一样使用hash_map了。
转自 http://hi.baidu.com/lr02/blog/item/7aca821c2294f78787d6b6a5.html