Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find hash_map header under Mac OSX

Tags:

c++

macos

header

#include <iostream>
#include <vector>
#include <list>

#ifdef __GNUC__
#include <ext/hash_map>
#else
#include <hash_map>
#endif

The compiler says " hash_map: No such file or directory " Need help. thank you.

like image 944
Josh Morrison Avatar asked Feb 09 '11 22:02

Josh Morrison


2 Answers

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>.

like image 95
Murilo Vasconcelos Avatar answered Oct 21 '22 16:10

Murilo Vasconcelos


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.

like image 40
templatetypedef Avatar answered Oct 21 '22 16:10

templatetypedef