Cannot find hash_map header under Mac OSX

On MacOSX the correct header is at <ext/hash_map> not <hash_map>. Here worked fine:

#if defined __GNUC__ || defined __APPLE__
#include <ext/hash_map>
#else
#include <hash_map>
#endif

int main()
{
        using namespace __gnu_cxx;

        hash_map<int, int> map;
}

By the way, I prefer to use <tr1/unordered_map>.


The <hash_map> header is not part of the C++ standard and is a compiler-specific implementation. There's no guarantee that you'll be able to find it on any particular system, or that the various implementations that arise on each system will be mutually compatible with one another.

If you want to use a hash map in C++, you might want to look into boost::unordered_map, tr1::unordered_map, or a prototype C++0x compiler's implementation of std::unordered_map. These implementations are fairly standardized, either by ISO or by the Boost community, and can easily be installed on most C++ compilers. I know that it's a bit presumptuous of me to just say "go rewrite this code using a different library," but given that C++ is about to gain hash containers of this form it's probably a worthwhile investment.

Tags:

C++

Macos

Header