C++ STL Map Erase Method

C++C++Beginner
Practice Now

Introduction

In this lab, we are going to learn about the erase() method in the C++ STL Map Container to delete a range of elements in a Map in the C++ programming language.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") subgraph Lab Skills cpp/variables -.-> lab-96230{{"`C++ STL Map Erase Method`"}} cpp/data_types -.-> lab-96230{{"`C++ STL Map Erase Method`"}} cpp/operators -.-> lab-96230{{"`C++ STL Map Erase Method`"}} cpp/for_loop -.-> lab-96230{{"`C++ STL Map Erase Method`"}} end

Create and fill the Map with key-value pairs of integers

Create a new C++ file in the ~/project directory named main.cpp using the command touch ~/project/main.cpp and open it using your preferred text editor.

In this step, we will create a map and fill it with key-value pairs of integers using the insert() method. The make_pair() function is used to insert a key value pair into the map. The keys are automatically sorted in the increasing order of keys.

#include <iostream>
#include <map>

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 2), in CPP  ===== \n\n\n";

    map<int, int> m;
    m.insert(make_pair(3, 30));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(5, 50));
    m.insert(make_pair(9, 90));
    m.insert(make_pair(1, 10));
}

Delete Map elements with keys less than a certain value

In this step, we will delete elements in the map with keys less than a certain value using the erase() method. In this example, we are deleting all the elements with keys less than 3. erase(m.begin(), m.find(3)) removes all the elements from the beginning of the map, up until the position of the iterator pointing to the element with key 3.

#include <iostream>
#include <map>

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 2), in CPP  ===== \n\n\n";

    map<int, int> m;
    m.insert(make_pair(3, 30));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(5, 50));
    m.insert(make_pair(9, 90));
    m.insert(make_pair(1, 10));

    cout << "Map elements before deletion:  " << endl;
    for (auto i : m)
    {
        cout << "( " << i.first << ", " << i.second << " ) ";
    }

    m.erase(m.begin(), m.find(3));

    cout << "\n\nMap elements after deletion:  " << endl;
    for (auto i : m)
    {
        cout << "( " << i.first << ", " << i.second << " ) ";
    }
    cout << "\n\n\n";
    return 0;
}

Compile and Run the code

To run the above C++ code, we need to compile and execute it using the following commands in the terminal:

g++ ~/project/main.cpp -o ~/project/main && ~/project/./main

Understand the Output

After successfully compiling and executing the above code, the output will be:

Welcome to Studytonight :-)


 =====  Program to demonstrate the working of erase() method in a Map (Part 2), in CPP  =====


Map elements before deletion:
( 1, 10 ) ( 2, 20 ) ( 3, 30 ) ( 5, 50 ) ( 9, 90 )

Map elements after deletion:
( 3, 30 ) ( 5, 50 ) ( 9, 90 )

Summary

In this lab, we learned about the erase() method in the C++ STL Map Container to delete a range of elements in a Map in the C++ programming language. We also learned how to create a Map and fill it with key-value pairs using the insert() method. Finally, we saw how to delete elements from a Map using erase().

Other C++ Tutorials you may like