I have an application (in C++) in which I need to have a set of pairings between Strings and Integers, i.e:
("david", 0)
("james", 1)
("helen", 2)
...
If we use the java (key, value) definition, I need to be able to (1) search to see if a key exists in the map and (2) retrieve the value associated with a given string (key).When working in java, I found that the HashMap type could handle everything that I needed.
I would like to do the same but in C++. I did some googling around and found that in the C++ 2011 library there is an unordered_map type that replicates this. I was curious as to whether this was the best approach.
In my application I have the following rules on the collection
I thought that instead of having both the integer and string stored, that instead store the strings in some list type and then return the index back, i.e. data = { "david", "james", "helen", ... }
and then something like find_Map(data, key) to return back the index(value) that it's located with. I thought this could be speed up by first sorting into ascending order and applying a searching alogrithm. But again, that's just a guess.
I do appreciate that this is a common problem and that many different approaches exist. I'm going to code up a few different ideas but thought it would be best to ask the group first to see what you guys thought.
A fast hash map/hash table (whatever you want to call it) for the C programming language. It can associate a key with a pointer or integer value in O(1) time.
Simple Hash Map (Hash Table) Implementation in C++ Hash table (also, hash map) is a data structure that basically maps keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the corresponding value can be found.
map uses a red-black tree as the data structure, so the elements you put in there are sorted, and insert/delete is O(log(n)). The elements need to implement at least operator< . hashmap uses a hash, so elements are unsorted, insert/delete is O(1).
Arrays can have duplicate values, while HashMap cannot have duplicated keys (but they can have identical values.) The Array has a key (index) that is always a number from 0 to max value, while in a HashMap, you have control of the key, and it can be whatever you want: number, string, or symbol.
you can use unordered_map<string,int>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With