There is a great example of a simple hash table in C++ for a single key, but I would like to hash on a <int, double> combination so that, for example h[5, 0.1] will return a double. Is this possible?
One possible method to get around this is to create an array of unordered_maps, and then have the key be a double. So, for example, I could simply call h[5][0.1] and get the double value back. In this the best way to go about this or can I create a multi-variabled key?
Sure. Make your key std::pair<int, double> (or tuple of <int, double>). Define appropriate hashing function (I would say hash(int) ^ hash(double) might work)
Related
I want to create a std::unordered_map < int, std::string >
or std::unordered_map< std::string, int >.
In this map I will store strings and their integer representations.
I'll fill this map only in the code(hard coded pairs).
I'll need convert input strings to their int values - find in map.
So I only need to search in the map at the run time.
At this point I need the best performance while converting.
In the best case - O(1).
My questions:
Should I use string as key or int ?
Should I define my own hash-function ?
What is the best-performance find function for the both cases string/int and int/string as key-pairs?
std::map or std::unordered_map or their multi-counterparts all are built up the same - they map a key (first template parameter) to a value (second one). If you want to get O(1) (unordered) or O(log(n)) (map) behaviour, you need to define as key that data type you want to get a value for.
As you want to find an integral value for a given string, the way to go in your case is std::unordered_map<std::string, int>.
A counter-example would be looking up names for error codes, there you typically have a specific error code returned by a function (or placed in errno) and want to get a string value for e. g. for printing the error to console. Then you'd have std::unordered_map<int, std::string> (provided you could not store the error strings in an array because of error codes being too far distributed...).
Edit:
Defining your own hash function is that kind of premature optimisation Konstantin mentions in his comment - std::string provides its own hash code function, which should fit well for most of the use cases. Only if you discover that your hashing gets too slow, try to find a faster way.
As all your strings are hard-coded, you might want to have a look at perfect hashing, e. g. in the gperf variant.
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?
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));
}
Hashing operation is irreversible (multiple strings can have the same hash value), so hash value cannot be used as a unique key and what you are trying to do is not possible.
I am looking for something like a school time table. Based on two input - Day of the week and Time of the day, one decides the subject. To implement this in c++, I was thinking of something like "map < pair < int, int>, int>".
I was reading here to use a key class and operator overloading.
Is there any other elegant way of doing it?
Thanks in Advance
That's not a map with two keys (which would allow you to look up items from knowledge of just one key), it's a composite key, and map<pair<day, time>, subject> should work just fine.
Also consider map<day, map<time, subject>>.
You could typedef it.
typedef pair<int, int> key;
map<key, int> myMap = new map<key, int>();
You can't create a map with 2 key values, you can create a map with a complex key value (composed of two types). However, this won't let you look at the map for say just day of week regardless of time of day. I would create a new class to use as key, and some helper functions to be able to work by just day or just time.
Also, for your use case Boost.MultiIndex may help you.
I have a problem with the creation of a hash of arrays. I need a Single Key - Multi Data system:
multimap <Type, vector<type> > var;
But how I can add elements to the vector?
Example: key = 3;
Now I need to append some elements into the vector whose key is 3.
Creating a temp-vector not an answer because I don't know when I need to input element into the vector with the current key.
sorry, understand my problem. i need fast-access struct, that will be operate with ~50,000 words with length ~20 each.
and i need something like tree.
also, have question:
how quick STL-structures, like vector,map,multimap and other?
What's wrong with std::map <KeyType, std::vector<SomeType> >, or some other collection as the value type? This gives you control over how to operate on the value collection. A multimap to me seems like a low-level form of std::map <KeyType, std::list<SomeType> >, but with none of the flexibility of a list.
To find the answer to your question you can look at the slides under point 6. at this site https://ece.uwaterloo.ca/~ece250/Lectures/Slides/
Hope that helps!
I need to describe an associative array in which to search, you can use the key and value. With functions add, delete, getBy1st (search by key), getBy2nd (search by value).
For example in C++:
symmap<std::string, int> m;
m.insert(make_pair<std::string,int> ("hello", 1));
m.insert(make_pair<std::string,int> ("wow", 2));
...
m.getBy1st("hello"); // returns 1
m.getBy2nd(2);// returns "wow"
It should work for O(log(n)) and store in std::pair .
I can not decide what the data structure used to store.
Maybe i can use some variation of rb-tree to store it?
This sounds a lot like Boost.Bimap.
Why not use a pair of hashtables to store the data - one hashing from T1 to T2 and the other hashing in the other direction?