How To Delete Multiple Key-Value Pairs From Multimap in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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: 90}, {Student 3: 92}, {Student 4: 69} } // deleting Student1 and Student2 Output: myMultimap = { {Student 3: 92}, {Student 4: 69} }Remove Multiple Key-Value Pairs From a MultimapWe can store the keys of the elements that we want to delete from the multimap in a vector. We can then iterate the vector and delete the elements associated with the keys using the std::multimap::erase() function. C++ Program to Erase Multiple Key-Value Pairs from a Multimap C++ // C++ Program to show how to delete Multiple Key-Value // pairs from the Multimap #include <iostream> #include <map> #include <vector> using namespace std; int main() { // Create a multimap multimap<int, string> myMultimap; myMultimap.insert(make_pair(1, "Apple")); myMultimap.insert(make_pair(2, "Banana")); myMultimap.insert(make_pair(1, "Mango")); myMultimap.insert(make_pair(3, "Peach")); myMultimap.insert(make_pair(2, "Grape")); // Create a vector to store keys of elements to be // deleted vector<int> keys = { 1, 2 }; // Iterate over the vector and delete elements from the // multimap for (int key : keys) { myMultimap.erase(key); } // Print the remaining elements in the multimap for (auto it = myMultimap.begin(); it != myMultimap.end(); ++it) { cout << it->first << " => " << it->second << '\n'; } return 0; } Output3 => Peach Time complexity: O(M log N), where N is the size of the multimap and M is the number of elements with the specified keys.Space Complexity: O(M) Comment More infoAdvertise with us Next Article How To Delete Multiple Key-Value Pairs From 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 Delete Multiple Key-Value Pairs from a Map in C++? In C++, a map container stores the collection of data in the form of a key and a value pair and this data is sorted on the basis of the key. In this article, we will learn how to delete multiple key-value pairs from a map in C++ STL. Example: Input: myMap = {{âappleâ, 1}, {âbananaâ, 2}, {âcherryâ, 3 2 min read How to Delete a Key-Value Pair from a Map in C++? In C++, maps are used to store key-value pairs in which each key is unique. In this article, we will learn how to delete a key-value pair from a map in C++. Example Input: mp={ {1,"One"}, {2,"Two"},{3,"Three"}}Key= 2Output: Map after deleting key: 1: One3: ThreeRemove a Key-Value Pair from Map in C+ 2 min read How to Add Multiple Key-Value Pairs to a Map in C++? In C++, a map is a container that stores elements in key-value pairs, where each key is unique and associated with a value. In this article, we will learn how to add multiple key-value pairs to a map in C++. Example: Input: myMap = { {1, "one"} }; Elements to be insert = { {2, "two"}, {3, "three"}} 2 min read How To Insert Multiple Key-Value Pairs Into a Multimap in C++? In C++, a multimap is similar to a map that stores the data in the key-value format and it also allows us to store duplicate keys for the same value. In this article, we will learn how to insert multiple Key-Value pairs efficiently into a multimap. Example: Input: multi_map = {{"Manas","Singing" }, 2 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 Like