Remove Elements from C++ Map

Beginner

Introduction

In this lab, you will learn how to use the erase() method to remove elements in a map in C++ STL. The map is a container that stores data in the key-value pair.

Create a Map

Create a map instance in main.cpp file by including these header files on the top of the file:

#include <iostream>
#include <bits/stdc++.h>

declare a map with integer keys and values as below:

map<int, int> m;

Insert data into the Map

Insert random data into the map using the insert() method. The make_pair() method can be used to assign key-value pairs to the map.

m.insert(make_pair(3, 9));
m.insert(make_pair(2, 4));
m.insert(make_pair(5, 25));
m.insert(make_pair(9, 81));
m.insert(make_pair(1, 1));

Remove an Element from Map

Use the erase(x) method to remove an element from the map with the key a value of x. If the value doesn't exist, it returns 0, otherwise 1.

int j = m.erase(5);

if(j == 1) {
    cout << "\nThe Map element with key 5 is deleted.";
} else {
    cout << "\nThe Map element with key 5 does not exist.";
}

You can also remove an element with the key value that does not exist in the map without resulting in the program crashing. For instance:

int k = m.erase(6);

if(k != 0){
    cout << "\n\nThe Map element with key 6 is deleted\n\n";
} else {
    cout << "\n\nThe Map element with key 6 does not exist.";
}

Run the code

To run the code, navigate to the directory containing main.cpp file on terminal and enter the following commands to compile and execute:

$ g++ main.cpp -o main
$ ./main

Complete code

Below is the complete code for your reference:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << "===== Program to demonstrate the working of erase() method in a Map (Part 1), in CPP ===== \n\n\n";

    cout << "*** The erase(x) method deletes the map element with key as x and returns 1 if found else returns 0. *** \n\n";

    // Map declaration (Map with key and value both as integers)
    map<int, int> m;

    // Filling the elements by using the insert() method.
    cout << "\n\nFilling the Map with key-value pairs of integers in random order."; //Map automatically stores them in increasing order of keys

    //make_pair() is used to insert a key-value pair into the map
    m.insert(make_pair(3, 9));
    m.insert(make_pair(2, 4));
    m.insert(make_pair(5, 25));
    m.insert(make_pair(9, 81));
    m.insert(make_pair(1, 1));

    cout << "\n\nThe number of elements in the Map are: " << m.size();

    cout << "\n\nThe elements of the Map m are: ";

    map<int, int>::iterator i;
    int j = 0;

    for (i = m.begin(); i != m.end(); i++)
    {
        cout << "( " << i->first << ", " << i->second << " ) ";
    }

    //Finding the map element with key 5
    j = m.erase(5);

    if (j == 1)
    {
        cout << "\n\nThe Map element with key 5 is deleted.";
    }
    else
    {
        cout << "\n\nThe Map element with key 5 does not exist.";
    }

    cout << "\n\nThe number of elements in the Map becomes: " << m.size();

    cout << "\n\nThe elements of the Map m after the erase operation are:  ";

    j = 0;

    for (i = m.begin(); i != m.end(); i++)
    {
        cout << "( " << i->first << ", " << i->second << " ) ";
    }

    j = m.erase(6);

    if (i != m.end())
    {
        cout << "\n\nThe Map element with key 6 is deleted\n\n";
    }

    else
    {
        cout << "\n\nThe Map element with key 6 does not exist.";
    }

    cout << "\n\n\n";

    return 0;
}

Summary

In this lab, you have started by creating a map and then filling it with data. Then, you have learned how to use the erase() method to remove elements from the map using their keys. We hope the above examples help you understand how to remove elements from a map in C++ STL.

Other Tutorials you may like