简体   繁体   中英

Cannot find hash_map header under Mac OSX

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM