How to Delete Multiple Key-Value Pairs from a Map in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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}}; Delete the key-value pairs {“apple”, 1}, {“cherry”, 3} Output: res={{"banana" : 2}}Deleting Multiple Key-Value Pairs from Map in C++If we want to delete multiple key-value pairs from a std::map, we can store the keys in the vector and then iterate the vector and keep deleting the keys using the std::map::erase() function. C++ Program to Delete Multiple Key-Value Pairs from Map C++ // C++ Program to illustrate how to delete multiple // key-value pairs from a map #include <iostream> #include <map> #include <vector> using namespace std; int main() { // Creating a map of string and int map<string, int> myMap = { { "apple", 1 }, { "banana", 2 }, { "cherry", 3 } }; // keys do be deleted vector<string> del({ "apple", "cherry" }); // deleting elements for (auto key : del) { myMap.erase(key); } // Displaying the map elements for (auto it = myMap.begin(); it != myMap.end(); ++it) { cout << it->first << " " << it->second << endl; } return 0; } Outputbanana 2 Time Complexity: O(M log N), where M is the number of elements to be deleted and N is the size of map.Space Complexity: O(M) Comment More infoAdvertise with us Next Article How to Delete Multiple Key-Value Pairs from a Map in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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: myMultim 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 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 Like