Open In App

How to Delete a Pair from a Multimap in C++?

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, multimap stores key-value pairs and for the same key there can be multiple values in a multimap. In this article, we will learn how to delete a pair from a multimap in C++.

Example

Input:
mpp={{"apple", 1},{"banana", 2},{"apple", 3}, {"orange", 4}}
keyToRemove=apple
valueToRemove=3
Output:
apple: 1
banana: 2
orange: 4

Remove a Pair from Multimap in C++

To delete a pair from multimap, we can use the std::multimap::erase() function with a combination of std::find_if().

First, create a custom binary predicate function to pass to the find_if() function to check for the matching key-value pair and find the iterator using the find_if() function. Then remove the pair using erase() by passing the iterator in it.

C++ Program to Delete a Pair from Multimap

C++




// C++ program to delete a pair from multimap
#include <algorithm>
#include <iostream>
#include <map>
  
using namespace std;
  
int main()
{
  
    // creating a mutimap
    multimap<string, int> myMpp = {
        { "apple", 1 },
        { "banana", 2 },
        { "apple", 3 },
        { "orange", 4 },
    };
  
    // Define the key and value to remove
    string keyToRemove = "apple";
    int valueToRemove = 3;
  
    // Find the element
    auto it = find_if(
        myMpp.begin(), myMpp.end(), [&](const auto& pair) {
            return pair.first == keyToRemove
                   && pair.second == valueToRemove;
        });
  
    // If found, erase it
    if (it != myMpp.end()) {
        myMpp.erase(it);
    }
  
    // Iterate and print remaining elements
    for (const auto& pair : myMpp) {
        cout << pair.first << ": " << pair.second << endl;
    }
  
    return 0;
}


Output

apple: 1
banana: 2
orange: 4

Time Complexity: O(N)
Auxiliary Space: O(1)

Note: We can also use equal_range() with erase() function to delete a pair from multimap.



Similar Reads

multimap::crbegin() and multimap::crend() in C++ STL
multimap::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator referring to the last element in the multimap container. Since multimap container contains the element in an ordered way, crbegin() will point to that element that will come last according to the container's sorting criterion. Syntax: multimap_name.crbeg
3 min read
multimap::cbegin() and multimap::cend() in C++ STL
multimap::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.cbegin()Param
3 min read
multimap::begin() and multimap::end() in C++ STL
multimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.begin() Parameters:
3 min read
How To Delete Multiple Key-Value Pairs From Multimap in C++?
In C++, a multimap is a container that stores elements where each element has a key value and a mapped value. Unlike maps, multimaps allow multiple key-value pairs with the same key. In this article, we will learn how to delete multiple key-value pairs from a multimap in C++. Example Input: myMultimap = { {Student 1: 90}, {Student 1: 78}, {Student
2 min read
Difference between pair in Multiset and Multimap in C++ STL
Pairs in C++: The pair container is a simple container defined in &lt;utility&gt; header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second). Pair is used to combine together two values which may be different in type. Pair provides a way to
5 min read
Group Vector of Pair Elements in Multimap
Multimap stores key-value pairs and for the same key, we can have more than one value i.e. multiple values for a single key. We can use this property of multimap to group vector of pair elements easily. In this article, we will discuss how to group elements of a vector of pairs in a multimap container. Example Input: myVector ={ {"A", 80},{"B", 70}
2 min read
How to Insert a Pair in Multimap in C++?
In C++, we have multimap which is used to store key-value pairs like a map but multimap can have multiple values for the same key. In this article, we will learn how to insert a pair into a multimap in C++. Example Input: myMultimap = { {1, "this"}, {2,"is"}} myPair = {2, "was"}; Output: myMultimap = { {1, "this"}, {2,"is"}, {2, "was"}}Inserting Pa
2 min read
How to Remove a Specific Pair From a Multimap in C++?
In C++, a multimap is a container that can store multiple key-value pairs, where each key can be associated with multiple values. In this article, we will learn how to remove all occurrences of a specific key-value pair from a multimap in C++. Example Input: myMultimap = {{1, “one”}, {2, “two”}, {2, “two”}, {3, “three”}}; Key-Value Pair to Remove =
2 min read
How to Replace a Specific Pair in a Multimap in C++?
in C++, multimap is similar to a map that stores the data in the key-value format where duplicate keys are allowed. In this article, we will learn how to replace a specific pair in a multimap in C++. Example Input: myMultimap = {{1, “one”}, {2, “two”}, {2, “two”}, {3, “three”}}; Key-Value Pair to Replace = {2, “two”}; To be Replaced with = {5, "fiv
3 min read
multimap::operator= in C++ STL
multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as parameter to the container written on left side of
2 min read
Article Tags :
Practice Tags :