5 Replies - 4380 Views - Last Post: 12 November 2011 - 09:18 PM Rate Topic: -----

#1 existence19   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-October 11

create hash function for STL hash map?

Posted 12 November 2011 - 05:53 PM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: create hash function for STL hash map?

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

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?
Was This Post Helpful? 0
  • +
  • -

#3 existence19   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-October 11

Re: create hash function for STL hash map?

Posted 12 November 2011 - 06:09 PM

C++ Standard Library
Was This Post Helpful? 0
  • +
  • -

#4 existence19   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-October 11

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
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

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.

This post has been edited by Oler1s: 12 November 2011 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: create hash function for STL hash map?

Posted 12 November 2011 - 09:18 PM

Some examples and tips.

Tip about STL hash_map and string
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1