Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashmap equivalent in C++

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

  1. The Integers are always sequential (as per the example) and start at 0.
  2. The Integer Values never change.
  3. The Map is created at the start of the application and doesn't change, i.e. it's Immutable.
  4. There are no duplicates of the string keys.
  5. Upon creation of the map, I don't know how many keys (and by extension integer values) I'm going to need to use. One of the parameters for my application is the directory of a text file which contains the list of words to be used.
  6. I don't care about start up time costs associated with this. I need the primary task (i.e. containsKey(..) and get(key) to be as fast as possible). And it will be called A LOT. The application is centered on processing large text corpora (i.e. Wikipedia) and forming co-occurence matrices between words/documents.

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.

like image 579
DavidG Avatar asked Jul 03 '17 05:07

DavidG


People also ask

Does C have a HashMap?

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.

What is the equivalent of HashMap in C++?

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.

Is map and HashMap same in C++?

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).

Is a HashMap like an array?

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.


1 Answers

you can use unordered_map<string,int>.

like image 182
Zaid Khan Avatar answered Sep 19 '22 21:09

Zaid Khan