![]() |
VOOZH | about |
in C++, multimap is similar to a map that stores the data in the key-value format where duplicate keys are allowed. In this article, we will learn how to replace a specific pair in a multimap in C++.
Example
Input: myMultimap = {{1, “one”}, {2, “two”}, {2, “two”}, {3, “three”}}; Key-Value Pair to Replace = {2, “two”}; To be Replaced with = {5, "five"} Output: myMultimap = {{1, “one”}, {3, “three”}, {5, "five"}, {5, "five"}};
To replace a specific pair in a std::multimap, we can use the std::multimap::equal_range function to get a range of iterators representing all occurrences of the key, find the required pair, delete it, and then insert a new one.
Multimap after replacing the key: 1 => one 3 => three 4 => four 4 => four
Time Complexity: O(K Log N), where K is the number of matching elements, and N is the size of the multimap.
Space Complexity: O(K)