Hashmap Implementation for negative numbers c++

How do I implement a hashmap using stl or any other method for a negative number in C++? A small description along with an example for the same would be appreciated.

I think you are talking about negatie indexes such as arr[-1] ,
You could use STL MAP in C++

now you could use arr[any int]=true or false;

Read more about MAP here : http://www.cplusplus.com/reference/map/map/map/

Hope it helps!

You can use normal array also by displacing the elements i.e if the max negative number(in magnitude) is suppose -20000 ,then choose 20000 as the displacement and add it to all numbers before hashing them. To retrieve the number just subtract the displacement from it (in this case 20000).

1 Like