How to Delete a Pair from an Unordered Map in C++? Last Updated : 06 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: OrangeRemove Key-Value Pair from Unordered Map in C++In unordered_map, we can delete a pair by using the std::unordered_map::erase() method. First, we check if a key for a pair that you want to delete exists or not. If it exists, then we remove it by simply passing that key to the erase() function. C++ Program to Delete a Pair from Unordered Map C++ // C++ program to delete pair from unordered map #include <iostream> #include <unordered_map> using namespace std; int main() { // Create an unordered_map unordered_map<int, string> myUnorderedMap; // Insert some elements myUnorderedMap[1] = "Apple"; myUnorderedMap[3] = "Mango"; myUnorderedMap[2] = "Orange"; // Find the iterator for key int key = 3; auto iter = myUnorderedMap.find(key); // Delete the pair with key if found if (iter != myUnorderedMap.end()) { myUnorderedMap.erase(iter); } // Display elements after deletion cout << "\nUnordered Map after deletion:" << endl; for (const auto& pair : myUnorderedMap) { cout << pair.first << ": " << pair.second << endl; } return 0; } Output Unordered Map after deletion: 2: Orange 1: Apple Time Complexity: O(N) is worst case time complexity, while the average complexity is O(1).Auxiliary Space: O(1) Note: We can also use clear() function to delete key-value pair from a unordered map. Comment More infoAdvertise with us Next Article How to Delete a Pair from an Unordered Map in C++? R rahulkatix28 Follow Improve Article Tags : C++ Programs C++ STL cpp-unordered_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 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 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 Create a Stack of Unordered_Map in C++? In C++, the stack is a container that follows the LIFO(Last In First Out) rule where new elements are added from one end (top) and removed from that end only. An unordered_map is an associative container that stores elements formed by a combination of key-value pairs, where the key should be unique. 2 min read How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read Like