I have to create a lookup table to work as points for the function name (in the form of keys) (as the value ). I am thinking of using STL's hash_map
container because access time for a hash table is O (1)
. Is there a good hash function for this? Currently I am using (31 * H + c)
as my hash function.
In addition, STL's hash_map
takes care of the collision, or do I have to take care of them in my code? Please give some examples if possible.
Sample code I currently
#include & lt; Iostream & gt; # Include & lt; Ext / hash_map> using namespace std; Namespace __gnu_cxx; Name space __gnu_cxx {#ifndef __HASH_STRING__ #define___HASH_STRING__ template & lt; & Gt; Structure hash & lt; String & gt; {Size_t operator () (const std :: string & amp;) const {size_t h = 0; Std :: string :: const_iterator p, p_end; For (p = s.begin (), p_end = s.end (); p! = P_end; ++ p) {h = 31 * h + (* p); } Returns H; }}; #end if }; Int main () {hash_map & lt; String, int & gt; Months; Months ["jan"] = 1; Months ["February"] = 2; Months ["march"] = 3; Months ["April"] = 4; Months ["May"] = 5; Months ["June"] = 6; Months ["July"] = 7; Months ["August"] = 8; Months ["September"] = 9; Months ["October"] = 10; Months ["November"] = 11; Months ["December"] = 12; Return 0; Assume that you have got full STL, it actually includes a hash function, hash < T & gt;
, which contains four * (c wire) in its included form, I do not know the details of its performance, but STL is generally engineered for acceptable display for most applications. To deal with the confrontation, which is for hashmap
, there is no need to worry about it.
Comments
Post a Comment