If you have tried to use some STL containers with GCC, such as hash_map:
// error: hash_map: No such file or directory
#include <hash_map>
int
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 <ext/hash_map>
#else
#include <hash_map>
#endif
namespace
std
{
using namespace
__gnu_cxx
;
}
int
main
()
{
std::
hash_map<
int
,
int
>
hm;
return
0
;
}
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 <map> but I get a bunch of template compile errors
when I change it to hash_map. Any suggestions? My program and the errors
are below...
#include <ext/hash_map>
#include <string>
using namespace std;
__gnu_cxx::hash_map<string, int> storage;
int main()
{
pair<string, int> item("blah", 1);
storage.insert(item);
}
When compiled I get the following:
g++ hm.cpp -o hm
/usr/include/c++/3.2.2/ext/stl_hashtable.h: In member function `size_t
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const [with _Val =
std: air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std: air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]':
/usr/include/c++/3.2.2/ext/stl_hashtable.h:522: instantiated from `size_t
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::_M_bkt_num(const _Val&, unsigned int) const [with _Val =
std: air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std: air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]'
/usr/include/c++/3.2.2/ext/stl_hashtable.h:887: instantiated from `void
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::resize(unsigned int) [with _Val = std: air<const std::string,
int>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>,
_ExtractKey = std::_Select1st<std: air<const std::string, int> >, _EqualKey
= std::equal_to<std::string>, _Alloc = std::allocator<int>]'
/usr/include/c++/3.2.2/ext/stl_hashtable.h:381: instantiated from
`std: air<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey,
_EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::insert_unique(const _Val&) [with _Val =
std: air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std: air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]'
/usr/include/c++/3.2.2/ext/hash_map:171: instantiated from
`std: air<__gnu_cxx::hashtable<std: air<const _Key, _Tp>, _Key, _HashFcn,
std::_Select1st<std: air<const _Key, _Tp> >, _EqualKey, _Alloc>::iterator,
bool> __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
_Alloc>::insert(__gnu_cxx::hashtable<std: air<con st _Key, _Tp>, _Key,
_HashFcn, std::_Select1st<std: air<const _Key, _Tp> >, _EqualKey,
_Alloc>::value_type&) [with _Key = std::string, _Tp = int, _HashFcn =
__gnu_cxx::hash<std::string>, _EqualKey = std::equal_to<std::string>, _Alloc
= std::allocator<int>]'
hm.cpp:14: instantiated from here
/usr/include/c++/3.2.2/ext/stl_hashtable.h:517: no match for call to `(const
__gnu_cxx::hash<std::string>) (const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&)'
make: *** [hm] Error 1
I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It
will compile if I use <map> but I get a bunch of template compile errors
when I change it to hash_map. Any suggestions? My program and the errors
are below...
#include <ext/hash_map>
#include <string>
using namespace std;
__gnu_cxx::hash_map<string, int> storage;
int main()
{
pair<string, int> item("blah", 1);
storage.insert(item);
}
When compiled I get the following:
g++ hm.cpp -o hm
/usr/include/c++/3.2.2/ext/stl_hashtable.h: In member function `size_t
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const [with _Val =
std: air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std: air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]':
/usr/include/c++/3.2.2/ext/stl_hashtable.h:522: instantiated from `size_t
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::_M_bkt_num(const _Val&, unsigned int) const [with _Val =
std: air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std: air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]'
/usr/include/c++/3.2.2/ext/stl_hashtable.h:887: instantiated from `void
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::resize(unsigned int) [with _Val = std: air<const std::string,
int>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>,
_ExtractKey = std::_Select1st<std: air<const std::string, int> >, _EqualKey
= std::equal_to<std::string>, _Alloc = std::allocator<int>]'
/usr/include/c++/3.2.2/ext/stl_hashtable.h:381: instantiated from
`std: air<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey,
_EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::insert_unique(const _Val&) [with _Val =
std: air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std: air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]'
/usr/include/c++/3.2.2/ext/hash_map:171: instantiated from
`std: air<__gnu_cxx::hashtable<std: air<const _Key, _Tp>, _Key, _HashFcn,
std::_Select1st<std: air<const _Key, _Tp> >, _EqualKey, _Alloc>::iterator,
bool> __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
_Alloc>::insert(__gnu_cxx::hashtable<std: air<con st _Key, _Tp>, _Key,
_HashFcn, std::_Select1st<std: air<const _Key, _Tp> >, _EqualKey,
_Alloc>::value_type&) [with _Key = std::string, _Tp = int, _HashFcn =
__gnu_cxx::hash<std::string>, _EqualKey = std::equal_to<std::string>, _Alloc
= std::allocator<int>]'
hm.cpp:14: instantiated from here
/usr/include/c++/3.2.2/ext/stl_hashtable.h:517: no match for call to `(const
__gnu_cxx::hash<std::string>) (const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&)'
make: *** [hm] Error 1
Mark
-----------------------------------------------------------------------------------------------------------------------------------
【3】
Re: gcc-3.0.2 + hash_map<string,int> compilation problems
- From : Stefan Olsson <stefan at noname4us dot com>
- To : Matthew Sundling <sundlm at cs dot wisc dot edu>
- 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<string,int> compilation problems
- Organization : Noname4us
- References : < [email protected] >
Hi!
I have had the same issue and solved it by declaring:
#include <string>
#include <ext/hash_map>
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<string, int>. 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 <iostream>
>#include <string>
>#include <ext/hash_map>
>
>int main () {
> std::hash_map<std::string, int > 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<const std::string, int>,
>_Key = std::string,
>_HashFcn = std::hash<std::string>,
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>,
>>
>_EqualKey = std::equal_to<std::string>,
>_Alloc = std::allocator<int>]':
>
>/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<const std::string, int>,
>_Key = std::string, _HashFcn = std::hash<std::string>,
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>,
>>
>_EqualKey = std::equal_to<std::string>,
>_Alloc = std::allocator<int>]'
>
>/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<const std::string, int>,
>_Key = std::string,
>_HashFcn = std::hash<std::string>,
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>,
>>
>_EqualKey = std::equal_to<std::string>,
>_Alloc = std::allocator<int>]'
>
>/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<const std::string, int>,
>_Key = std::string,
>_HashFcn = std::hash<std::string>,
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>,
>>
>_EqualKey = std::equal_to<std::string>,
>_Alloc = std::allocator<int>]'
>
>/s/unsup/include/g++-v3/ext/hash_map:173: instantiated from
>`_Tp& std::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
>_Alloc>::operator[](typename std::hashtable<std::pair<const _Key,
>_Tp>, _Key, _HashFcn, std::_Select1st<std::pair<const _Key, _Tp>
>
>>, _EqualKey, _Alloc>::key_type&)
>>
>[with _Key = std::string,
>_Tp = int,
>_HashFcn = std::hash<std::string>,
>_EqualKey = std::equal_to<std::string>,
>_Alloc = std::allocator<int>]'
>
>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<std::string>) (const std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >&)'
>
>
>Thanks!
>Matt
>
>------------------
>Matthew Sundling
>[email protected]
>
>
>
看了上面三个资料后,我想你可以像使用一个map一样使用hash_map了。
转自 http://hi.baidu.com/lr02/blog/item/7aca821c2294f78787d6b6a5.html