Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a hash map without original key values

Tags:

c++

hash

I have a file containing a list of couples. Each couple contains a 64 bit integer, that is an hash computed over a string, and a value that is an integer.

I want to load the list in a hash map in the main memory. And then search values using the original string (I have the hash function).

For Example the file could be like:

keys values
78243536 12
38735342 20
32353498 18

if I want to search a term for example "pippo" I can use hashfunction("pippo").

The question is: can I use std::map computed on these hash? How can I insert directly the hash values inside the map without having the original string?

like image 524
Daniele Avatar asked Mar 26 '26 18:03

Daniele


1 Answers

A map is a mapping between keys of a given type and values. The interface of the map is defined in a way that you may either iterate through the whole map or search for a specific element using the key in the map. In your case you want to be able to search not by a key in the map but using another element that is somehow relevant to this key. You can not do this with bare map but you can create a helper function to handle that for you:

map<long long, int> my_map;

.... so some stuff ...

map<long long, int>::iterator find_by_string(const std::string& s) {
  return my_map.find(hash_code(s));
}
like image 195
Ivaylo Strandjev Avatar answered Mar 29 '26 10:03

Ivaylo Strandjev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!