Hi
I have to create a hash map with key as string and data as string. i have to create a custom hash function for the hash map, but i don't know how to do that.. can somebody explain and give an example from which i could understand?
create hash function for STL hash map?
Page 1 of 15 Replies - 4380 Views - Last Post: 12 November 2011 - 09:18 PM
Replies To: create hash function for STL hash map?
#2
Re: create hash function for STL hash map?
Posted 12 November 2011 - 05:56 PM
Are you using the STL or the C++ Standard Library?
#3
Re: create hash function for STL hash map?
Posted 12 November 2011 - 06:09 PM
C++ Standard Library
#4
Re: create hash function for STL hash map?
Posted 12 November 2011 - 06:15 PM
sorry... I have to use the hash_map in STL for which i have to write a custom hash function. I know how to write hash functions but don't know how to write hash function to be used in the STL hash_map.
i am using c++ do write the program
i am using c++ do write the program
#5
Re: create hash function for STL hash map?
Posted 12 November 2011 - 06:22 PM
The C++ standard library, from the 2003 standard, has no hash map, which is why you can't specify a hash function for the map.
If you are making use of TR1 or targetting the C++11 standard, your compiler may have unordered_map available. The constructor allows you specify a Hash functor.
If you the above two are not true, you have one of two options. You have to go to Boost for its unordered map implementation, but if you are already using Boost, this isn't a problem. Otherwise, you have to go to another third party library or write your own.
EDIT
It's similar with the STL hash_map. The constructor has an argument for a hash function. That's really a hash functor (i.e. a struct with the () operator overloaded).
EDIT2:
EDIT 2 : In case it's still not clear, refer to the documentation.
Look at hash_map. Notice that hash parameter in the template? You need to supply your own created template struct. You have to create the hash functor. Scroll down to members to see what you have to implement.
If you are making use of TR1 or targetting the C++11 standard, your compiler may have unordered_map available. The constructor allows you specify a Hash functor.
If you the above two are not true, you have one of two options. You have to go to Boost for its unordered map implementation, but if you are already using Boost, this isn't a problem. Otherwise, you have to go to another third party library or write your own.
EDIT
It's similar with the STL hash_map. The constructor has an argument for a hash function. That's really a hash functor (i.e. a struct with the () operator overloaded).
EDIT2:
EDIT 2 : In case it's still not clear, refer to the documentation.
Look at hash_map. Notice that hash parameter in the template? You need to supply your own created template struct. You have to create the hash functor. Scroll down to members to see what you have to implement.
This post has been edited by Oler1s: 12 November 2011 - 06:29 PM
#6
Re: create hash function for STL hash map?
Posted 12 November 2011 - 09:18 PM
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|