Signup/Sign In

C++ Using erase() method in STL Multimap (Part 2)

Hello Everyone!

In this tutorial, we will learn about the working of the erase() method in a Map in STL (Part 2) in the C++ programming language.

To understand the basic functionality of the Map Container in STL, we will recommend you to visit https://www.studytonight.com/cpp/stl/stl-container-map, where we have explained this concept in detail from scratch.

What is Multimap?

Multimap is similar to map with two additional functionalities:

  1. Multiple elements can have the same or duplicate keys.

  2. Multiple elements can have the same or duplicate key-value pair.

In Multimap, erase(m.begin(), m.find(x)) 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 concept erase() method in Multimap (Part 2), in CPP  ===== \n\n\n";

    cout << " In Multimap, erase(m.begin(), m.find(x)) deletes all the elements with key smaller than key x.\n\n";

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

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

    //make_pair() is used to insert a key value pair into the Multimap
    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));
    m.insert(make_pair(6, 50));
    m.insert(make_pair(3, 60));

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

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

    multimap<int, int>::iterator i;

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

    //Copying one multimap into another
    multimap<int, int> m1(m.begin(), m.end());

    //Deleting all the elements with key smaller than 5
    m1.erase(m1.begin(), m1.find(5));

    cout << "\n\nThe elements of the Multimap m1 after deleting all the elements with key smaller than as 5 are:\n ";

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

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

    return 0;
}

Output:

C++ erase() Multimap 2

We hope that this post helped you develop a better understanding of the concept of the erase() method in the Multimap 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 : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.