![]() |
VOOZH | about |
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} }
We 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.
3 => 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)