I’m relatively new to C++. In Java, it’s easy for me to instantiate and use a hashmap. I’d like to know how to do it in a simple way in C++, since I saw many different implementations and none of them looked simple to me.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Most compilers should define
std::hash_mapfor you; in the comingC++0xstandard, it will be part of the standard library asstd::unordered_map. The STL Page on it is fairly standard. If you use Visual Studio, Microsoft has a page on it.If you want to use your class as the value, not as the key, then you don’t need to do anything special. All primitive types (things like
int,char,booland evenchar *) should ‘just work’ as keys in ahash_map. However, for anything else you will have to define your own hashing and equality functions and then write ‘functors’ that wrap them in a class.Assuming your class is called
MyClassand you have already defined:You will need to define two functors to wrap those methods in objects.
And instantiate your
hash_map/hash_setas:Everything should work as expected after that.