How to Remove a Specific Pair From a Multimap in C++? Last Updated : 05 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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, “two”}; Output: myMultimap = {{1, “one”}, {3, “three”}};Remove a Specific Pair From a Multimap in C++To remove a specific pair from a std::multimap, we can use the std::multimap::equal_range function to get a range of iterators representing all occurrences of the key, and then use the std::multimap::erase function to remove each occurrence of the key-value pair. C++ Program to Remove a Specific Pair From a Multimap C++ // C++ program to illustrate how to remove the specific key // value pair from a multimap #include <iostream> #include <map> using namespace std; int main() { // Creating a multimap multimap<int, string> myMultimap = { { 1, "one" }, { 2, "two" }, { 2, "two" }, { 3, "three" } }; // Key-value pair to remove int keyToRemove = 2; string valueToRemove = "two"; // Getting the range of the key auto range = myMultimap.equal_range(keyToRemove); // Removing all occurrences of the key-value pair for (auto it = range.first; it != range.second;) { if (it->second == valueToRemove) { it = myMultimap.erase(it); } else { ++it; } } // Printing the multimap after removing the key-value // pair cout << "Multimap after removing the key-value pair:" << endl; for (const auto& pair : myMultimap) { cout << pair.first << " => " << pair.second << endl; } return 0; } OutputMultimap after removing the key-value pair: 1 => one 3 => three Time complexity: O(M *log N), where M is the count of elements associated with a particular key and N is the total number of elements in the multimap.Space Complexity: O(N) Comment More infoAdvertise with us Next Article How to Create a Stack of Multimap in C++? S sravankumar_171fa07058 Follow Improve Article Tags : C++ Programs C++ STL cpp-multimap CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 Re 3 min read How to Delete a Pair from a Multimap in C++? 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=applevalueToRemove=3Output:apple: 2 min read How to Remove a Key-Value Pair from Map in C++? In C++, a map is an associative container that stores elements in key-value pairs, where the key values are unique. In this article, we will learn how to remove a specific key-value pair from a map in C++. Example: Input : myMap = {{1, 10}, {3, 30}, {2, 20}, {4, 40}};key = 2Output :Map After Removal 2 min read How to Remove an Element from a Set in C++? In C++, sets are a type of associative container in which each element has to be unique. The values are stored in a specific sorted order i.e. either ascending or descending. In this article, we will see how to remove specific elements from a set in C++. Example Input: set = {100,120,12,56,78,9,32,4 2 min read How to Create a Stack of Multimap in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A multimap is a container that stores key-value pairs in an ordered manner. In this article, we will learn how to c 2 min read How to Create a Multimap of Arrays in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr 2 min read Like