How to Remove a Key-Value Pair from Map in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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:1: 103: 304: 40Remove a Key from a Map in C++To remove a specific key-value pair from a std::map, we can use the std::map::erase function. This function removes the key-value pair associated with the given key from the map. C++ Program to Remove a Key-Value Pair from MapThe below program demonstrates how we can remove a specific key-value pair from a map in C++ STL. C++ // C++ Program to illustrate how to Remove a Specific // Key-Value Pair from a Map #include <iostream> #include <map> using namespace std; int main() { // Create a map with integer keys and values map<int, int> myMap = { { 1, 10 }, { 3, 30 }, { 2, 20 }, { 4, 40 }, { 5, 50 } }; // Specify the key you want to remove int keyToRemove = 4; // Find the iterator for the specified key auto it = myMap.find(keyToRemove); // Check if the key is found if (it != myMap.end()) { // Erase the key-value pair from the map myMap.erase(it); } else { // Output if the key is not found cout << "Key " << keyToRemove << " not found in the map." << endl; } cout << "Map After Removal:" << endl; // Print the updated map for (const auto& pair : myMap) { cout << pair.first << ": " << pair.second << endl; } return 0; } OutputMap After Removal: 1: 10 2: 20 3: 30 5: 50 Time Complexity: O(log N), here N is the number of elements in the mapSpace Complexity: O(1) Comment More infoAdvertise with us Next Article How to Remove a Key-Value Pair from Map in C++? subhra_ghosh Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 Replace a Key-Value Pair in a Map in C++? A map in C++ is a part of the Standard Template Library (STL) that allows the user to store data in key-value pairs where each key in a map must be unique. In this article, we will learn how to replace a specific key-value pair in a Map in C++. Example: Input : map<int, string> mp={{1,"John"}, 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 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 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 Like