Q: How to use the hash_map class in C++
A: If you have Visual Studio .NET 2003 you can try the 'hash_map' class instead of the map if you want to use hash tables instead of binary trees to store your pairs 'key -> value'. There follows a example of a hash_map.
I know that it's a lot easier to use the hash_map from the SGI's STL implementation, but if you must use Visual Studio .NET 2003 you'll need to know this.
Code:#include <string> #include <hash_map> #include <iostream> // The following class defines a hash function for strings class stringhasher : public stdext::hash_compare <std::string> { public: /** * Required by * Inspired by the java.lang.String.hashCode() algorithm * (it's easy to understand, and somewhat processor cache-friendly) * @param The string to be hashed * @return The hash value of s */ size_t operator() (const std::string& s) 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); } return h; } /** * * @param s1 The first string * @param s2 The second string * @return true if the first string comes before the second in lexicographical order */ bool operator() (const std::string& s1, const std::string& s2) const { return s1 < s2; } }; typedef stdext::hash_map<std::string, std::string, stringhasher> HASH_S_S; /** * Compile this program with "cl -GX hash_sample.cpp" * if using MSVC++ 7.1 (Visual Studio .NET 2003) */ int main (int argc, char *argv[]) { HASH_S_S hm; HASH_S_S::iterator it; //-- Inserting the names of months in a hash map //-- key = names of the months in Portuguese //-- value = names of the months in English hm[std::string("janeiro")] = std::string("January"); hm[std::string("fevereiro")] = std::string("February"); hm[std::string("março")] = std::string("March"); hm[std::string("abril")] = std::string("April"); hm[std::string("maio")] = std::string("May"); hm[std::string("junho")] = std::string("June"); hm[std::string("julho")] = std::string("July"); hm[std::string("agosto")] = std::string("August"); hm[std::string("setembro")] = std::string("September"); hm[std::string("outubro")] = std::string("October"); hm[std::string("novembro")] = std::string("November"); //-- Searching for the translation of the months "março" and "dezembro" in the map //-- (dezembro was not put into the map, so you can not find the corresponding element "December") it = hm.find(std::string("março")); if(it != hm.end()) std::cout << "The value corresponding to the key 'março' is " << it->second << std::endl; else std::cout << "The value corresponding to the key 'março' was not found" << std::endl; it = hm.find(std::string("dezembro")); if(it != hm.end()) std::cout << "The value corresponding to the key 'dezembro' is " << it->second << std::endl; else std::cout << "The value corresponding to the key 'dezembro' was not found" << std::endl; //-- Listing the pairs key, value (they're not ordered.) for(it = hm.begin(); it != hm.end(); ++it) { std::cout << "Key = \'" << it->first << "\' -> value = \'" << it->second << "\'" << std::endl; } }


Reply With Quote
Bookmarks