Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Implement Map in STL using C++



A map is an associative container that stores elements in a key-value pair format. Each element has a unique key and a corresponding mapped value. No two mapped values can have the same key. In this article, we will learn how to use a map from the Standard Template Library (STL) in C++.

What is Map?

Map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted automatically, and each key is associated with a value. You can insert, access, and remove elements based on the key.

For example, we can store fruit names and their prices, and easily get the price by using the fruit's name:

map<string, int> fruitPrices;
fruitPrices["Apple"] = 30;
fruitPrices["Banana"] = 20;
fruitPrices["Cherry"] = 50;

// Accessing the price of Apple
cout << "Price of Apple: " << fruitPrices["Apple"] << endl;

//Output will be displayed as:
Price of Apple: 30

Using map Class in STL

The <map> class is part of C++ standard STL library. It automatically stores key-value pairs in sorted order based on the key. Below are some functions used in map class:

  • find(): Searches for a key in the map and returns the position of the element. If it's not found, it gives the position where it would be at the end.
  • erase(): Removes the key-value pair from the map using the specified key.
  • equal_range(): Returns a pair of positions showing where elements with the same key are located.
  • insert(): Adds a new key-value pair into the map.
  • size(): Tells you how many items (key-value pairs) are in the map.
  • count(): Checks if a key exists in the map. It returns 0 if not found and 1 if it's there.

Steps to Implement Map in C++ STL

Following are steps/algorithm to implement a map using C++ STL:

  • Create a map using std::map.
  • Insert key-value pairs using insert() or [] operator.
  • Access elements using the [] operator.
  • Remove elements using erase().
  • Iterate and display map elements using a loop.

C++ Program to Implement Map using STL

The below code is implementation of the above algorithm in C++ language.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    map<int, string> students;

    // Insert elements
    students[101] = "Rahul";
    students[102] = "Priya";
    students[103] = "Amit";

    // Display map elements
    cout << "Student Records:" << endl;
    for (auto& entry : students) {
        cout << "Roll No: " << entry.first << ", Name: " << entry.second << endl;
    }

    // Remove an element
    students.erase(102);

    cout << "After removing roll number 102:" << endl;
    for (auto& entry : students) {
        cout << "Roll No: " << entry.first << ", Name: " << entry.second << endl;
    }

    return 0;
}

The output of above code will be

Student Records:
Roll No: 101, Name: Rahul
Roll No: 102, Name: Priya
Roll No: 103, Name: Amit
After removing roll number 102:
Roll No: 101, Name: Rahul
Roll No: 103, Name: Amit

Time and Space Complexity

Time Complexity:

  • Insertion, Deletion, and Search: O(log n) because the map is implemented as a balanced binary search tree.
  • Iteration: O(n) as we visit each element once.

Space Complexity: O(n) where n is the number of elements in the map.

Updated on: 2025-05-07T18:29:15+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements