LAST UPDATED: NOVEMBER 1, 2020
C++ Using erase() method in STL Map (Part 2)
Hello Everyone!
In this tutorial, we will learn about the working of the erase() method to delete a range of elements in a Map in STL in the C++ programming language.
To understand the basic functionality of the Map Container in STL, we will recommend you to visit the C++ STL Map Container, where we have explained this concept in detail from scratch.
In a Map, erase(m.begin(), m.find(x))
method deletes all the elements with a key smaller than the key x
.
For a better understanding of its implementation, refer to the well-commented C++ code given below.
Code:
#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 2), in CPP ===== \n\n\n";
cout << "*** The erase(m.begin(),m.find(x)) method deletes the map element with key less than x. *** \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, 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 << "\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 << " ) ";
}
//Deleting the map elements with key less than 3
m.erase(m.begin(), m.find(3));
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: ";
for (i = m.begin(); i != m.end(); i++)
{
cout << "( " << i->first << ", " << i->second << " ) ";
}
cout << "\n\n\n";
return 0;
}
Output:
We hope that this post helped you develop a better understanding of the concept of the erase()
method to delete a range of elements in the Map Container in STL and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )