How to Delete a Key-Value Pair from a Map in C++? Last Updated : 06 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++To delete a key-value pair from a map, we can use the std::map::erase() method. First, we will check if a key for a key-value pair that you want to delete exists or not. If it exists, we then remove it by simply passing the key to the erase() function. C++ Program to Delete Key-Value Pair from Map C++ // C++ program to delete the key-value pair from a map #include <iostream> #include <map> using namespace std; int main() { // Creating a map map<int, string> myMap; // Inserting key-value pairs into the map myMap[1] = "One"; myMap[2] = "Two"; myMap[3] = "Three"; // Displaying the map before deletion cout << "Map before deletion:" << endl; for (const auto& pair : myMap) { cout << pair.first << ": " << pair.second << endl; } // Finding the iterator for the key to be deleted int key = 2; auto it = myMap.find(key); // Deleting the key-value pair using erase() if (it != myMap.end()) { myMap.erase(it); } // Displaying the map after deletion cout << "\nMap after deletion:" << endl; for (const auto& pair : myMap) { cout << pair.first << ": " << pair.second << endl; } return 0; } OutputMap before deletion: 1: One 2: Two 3: Three Map after deletion: 1: One 3: Three Time Complexity: O(log N)Space Complexity: O(1) Note: We can also use clear() function to delete key-value pair from a map. Comment More infoAdvertise with us Next Article How to Delete a Pair from a Multimap in C++? R rahulkatix28 Follow Improve Article Tags : C++ Programs C++ STL cpp-map 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 Pair from an Unordered Map in C++? In C++, the unordered_map is like a dictionary that stores data in the form of key-value pairs. In this article, we will learn how to delete a key-value pair from an unordered_map in C++. Example Input:mp={ {1,"Apple"}, {3,"Mango"},{2,"Orange"}}Key= 3Output:Map after deleting key:1: Apple2: OrangeRe 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 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 Find First Key-Value Pair in a Map in C++? In C++ STL, a map is a container that stores key-value pairs in an ordered or sorted manner. In this article, we will learn how to find the first key-value pair in a Map. Example: Input: myMap = {{1, "C++"}, {2, "Java"}, {3, "Python"}, {4, "JavaScript"}}; Output: First key-Value Pair : (1, C++)Getti 2 min read Like