Implementation of Hash Maps in C++

Last Updated : 3 Jun, 2026

A HashMap is a data structure that stores data as key-value pairs, allowing values to be accessed quickly using their corresponding keys. In C++, HashMaps are implemented using the unordered_map container, which uses hashing for efficient data storage and retrieval.

  • Each key must be unique.
  • Provides average O(1) time complexity for insertion, deletion, and search operations.
  • Uses hashing for fast access and does not maintain elements in sorted order.
C++
#include <iostream>
#include <unordered_map>
using namespace std;

int main(){

    unordered_map<string, int> fruits;

    fruits["Apple"] = 10;
    fruits["Mango"] = 20;
    fruits["Cherry"] = 30;

    cout << "Apple : " << fruits["Apple"] << endl;
    cout << "Mango : " << fruits["Mango"] << endl;
    cout << "Cherry : " << fruits["Cherry"] << endl;

    return 0;
}

Output
Apple : 10
Mango : 20
Cherry : 30

Explanation:

  • This program creates an unordered_map named fruits to store fruit names as keys and their quantities as values.
  • It inserts three key-value pairs into the HashMap and then retrieves and displays the values associated with each fruit name.

Header File Required

To use HashMaps in C++, include the following header file:

#include <unordered_map>

Syntax

unordered_map<key_type, value_type> hashMap;

How Hash Maps Work

A HashMap uses a hash function to convert a key into a numerical value called a hash code. The hash code determines the location (bucket) where the key-value pair will be stored.

Components of Hashing

Components-of-Hashing

In above diagram:

  • Key: The input data used to identify a value (e.g., Apple, Mango, Cherry).
  • Hash Function: Converts a key into a hash value or bucket index.
  • Hash Table: Stores the actual key-value pairs at the computed locations.
  • The hash function helps locate data quickly without searching the entire table.

Suppose we store:

KeyValue
Apple10
Mango20
Cherry30

The hash function may generate:

KeyHash CodeBucket
Apple1055
Mango2122
Cherry3188

When searching for "Mango", the hash function is applied again, directly locating bucket 2 instead of scanning all elements.

Example: Common HashMap Operations

C++
#include <iostream>
#include <unordered_map>
using namespace std;

int main() {

    unordered_map<string, int> mp;

    // Insert
    mp["Apple"] = 10;
    mp["Mango"] = 20;
    mp["Cherry"] = 30;

    // Access
    cout << "Apple Value: "
         << mp["Apple"] << endl;

    // Search
    if(mp.find("Mango") != mp.end())
    {
        cout << "Mango Found" << endl;
    }

    // Traverse
    cout << "\nElements:\n";

    for(auto pair : mp)
    {
        cout << pair.first
             << " -> "
             << pair.second
             << endl;
    }

    // Delete
    mp.erase("Cherry");

    cout << "\nSize After Deletion: "
         << mp.size();

    return 0;
}

Explanation:

  • Creates a HashMap named mp to store fruit names as keys and quantities as values.
  • Inserts three key-value pairs: Apple → 10, Mango → 20, and Cherry → 30.
  • Accesses and searches data by displaying Apple's value and checking if Mango exists.
  • Traverses the HashMap using a loop to print all key-value pairs.
  • Deletes Cherry from the HashMap and displays the updated number of elements.

Time Complexity Analysis

OperationAverage CaseWorst Case
InsertionO(1)O(n)
SearchO(1)O(n)
DeletionO(1)O(n)
AccessO(1)O(n)
TraversalO(n)O(n)
Comment