How to Replace a Specific Pair in a Multimap in C++?
Last Updated :
05 Mar, 2024
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"}};
Replace a Specific Pair in a Multimap in C++
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.
Approach
- Get the range of the old key using the equal_range() function.
- Iterate over the range of the old key.
- In each iteration, check if the value of the current pair matches the value you want to replace.
- If it matches, insert a new pair into the multimap with the new key and new value.
- Remove all occurrences of the old key-value pair from the multimap using erase() function.
C++ Program to Replace a Specific Pair in a Multimap
C++
// C++ Program to illustrate how to replace a specific
// key-value pair
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Creating a multimap
multimap<int, string> myMultimap = { { 1, "one" },
{ 2, "two" },
{ 2, "two" },
{ 3, "three" } };
// Key-value pair to replace
int oldKey = 2;
string value = "two";
// New key
int newKey = 4;
string newValue = "four";
// Getting the range of the old key
auto range = myMultimap.equal_range(oldKey);
// Inserting new pairs with the new key and the same
// value
for (auto it = range.first; it != range.second; ++it) {
if (it->second == value) {
myMultimap.insert({ newKey, newValue });
}
}
// Removing all occurrences of the old key-value pair
myMultimap.erase(oldKey);
// Printing the multimap after replacing the key
cout << "Multimap after replacing the key:" << endl;
for (const auto& pair : myMultimap) {
cout << pair.first << " => " << pair.second << endl;
}
return 0;
}
OutputMultimap 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)
Similar Reads
How to Remove a Specific Pair From a Multimap in C++? In C++, a multimap is a container that can store multiple key-value pairs, where each key can be associated with multiple values. In this article, we will learn how to remove all occurrences of a specific key-value pair from a multimap in C++. Example Input: myMultimap = {{1, âoneâ}, {2, âtwoâ}, {2,
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 Insert a Pair in Multimap in C++? In C++, we have multimap which is used to store key-value pairs like a map but multimap can have multiple values for the same key. In this article, we will learn how to insert a pair into a multimap in C++. Example Input: myMultimap = { {1, "this"}, {2,"is"}} myPair = {2, "was"}; Output: myMultimap
2 min read
How To Find All Occurrences of a Key in a Multimap in C++? In C++, multimaps are associative containers similar to maps, but unlike maps, they can store multiple values mapped to the same key. In this article, we will learn how to find all the occurrences of a specific key in a multimap in C++. Example: Input:myMutimap = {{ "id", "111" }, { "id", "112" }, {
2 min read
How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5,
2 min read
How to Store Maps in a Map in C++? In C++, a map is a container that stores elements as a key value and a mapped value. A set is a container that stores unique elements in a particular order. In this article, we will learn how to create a map of sets in C++ STL. Example: myMap: { {1, {{1, "Apple"}, {2, "Banana"}}}, {2, {{3, "Cherry"}
2 min read