Hashing is a traditional approach in computer science whereby data structures called hash maps are used to store key-value pairs_. _Apart from storing key-value pairs, hash maps can be used to solve a wide variety of problems in computer science. When used with priority queues and other container adapters like stacks and queues, hash maps can make problem-solving faster by reducing the running time complexity.

Let’s dive into the basic concept of hash maps, their usage in C++, and applications to some of the most important problems.


The Concept Behind Hash Maps

Hash maps generally store elements as key-value pairs. Consider this example: We want to store a list of grocery items with their prices into hash maps. The name of the item serves as a key and its price is the value. This key-value pair is stored into a hash map, as shown below:

Image for post

Items with prices stored in unordered maps.

The order of storing the key-value is not guaranteed in an Unordered Hash Map (also called an Unordered Map). When we insert an item into an Unordered Hash Map, it can reside at any position in the Map object. If we want strict ordering of items in ascending order of their keys, we can use an Ordered Hash Map or simply Maps. For the same example as above, this is how the items are stored in Maps:

Image for post

Items with prices stored in Maps.

The keys are sorted in lexicographicorder starting with the smallest letter, c. When a new item like Juicewith a price of$12needs to be added to Maps, it is inserted at position 3 afterChocolateto maintain the lexicographic order_._

Image for post

Juice added to Maps.

#programming #startup #data-structures #computer-science #software-development #data science

The Importance of Hash Maps
1.15 GEEK