I have several GUIDs and I’d like to implement a hash table to quickly retrieve them. How would I do that?
If I treat the GUIDs as hash codes I need to do something like
index = GUID % prime_number_that_covers_all_GUID_bits
but I’m unsure if this is the right way to do it. How should I do to realize such a hash table?
You could use
std::unordered_map, which takes aKeytype (GUID) in your case, and aValuetype, which could be some user info or program info (depending on your app). Storing is as simple as calling the member functionsinsert()oremplace()and looking up a stored value is done by callingfind().The example below uses
std::stringas the underlying type for your keys, and implicitlystd::hash<std::string>as the hash function. For other GUID types, you might need to roll your own hash function object and pass that as a template parameter to the hash table.Output on LiveWorkSpace