How to Find the Union of Two Multimaps in C++?
Last Updated :
12 Mar, 2024
In C++, finding the union of two multimaps consists of combining the elements from both multimap collections while considering the duplicates as a multimap allows multiple values to have the same key. In this article, we will learn to find the union of two multimaps in C++.
Example:
Input:
multi1 = {1, "Java"}, {2, "Python"}, {3, "C++"}, {4, "JavaScript"}
mutli2 = {2, "Python"}, {4, "JavaScript"}, {5, "TypeScript"}
Output:
Union MultiMap:
1: Java
2: Python
2: Python
3: C++
4: JavaScript
4: JavaScript
5: TypeScript
Finding Union of Two Multimaps in C++
To find the union of two multimaps in C++, we can insert all the elements of the two multimaps in the new multimap container using std::multimap::insert() function that inserts elements from a range to the multimap container.
C++ Program to Find the Union of Two Multimaps
The below program demonstrates how we can use the std::insert() function to find the union of two multimaps in C++ STL.
C++
// C++ Program to illustrate how to find the union of two multimaps
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Defining multimap1
multimap<int, string> multimap1 = { { 1, "Java" },
{ 2, "Python" },
{ 3, "C++" },
{ 4, "JavaScript" } };
// Defining multimap2
multimap<int, string> multimap2 = { { 2, "Python" },
{ 4, "JavaScript" },
{ 5, "TypeScript" } };
// Finding union between multimap1 and multimap2
multimap<int, string> unionMap = multimap1;
unionMap.insert(multimap2.begin(), multimap2.end());
// Printing union
cout << "Union MultiMap:" << endl;
for (const auto& pair : unionMap) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
OutputUnion MultiMap:
1: Java
2: Python
2: Python
3: C++
4: JavaScript
4: JavaScript
5: TypeScript
Time complexity: O(N+M), where N and M are the size of the two multimaps.
Auxiliary Space: O(N+M)
Note: We can also use the std::merge
function from the <algorithm>
library to find the union of two multimaps in C++.
Similar Reads
How to Find the Union of Two Maps in C++? In C++, maps are associative containers that store key-value pairs. The union of two maps means combining their elements while ensuring that duplicate keys are handled appropriately. In this article, we will learn how to find the union of two maps in C++. For Example, Input: mapA = {{'a', 1}, {'b',
2 min read
How to Find Union of Two Multisets in C++? In C++, multisets are a type of associative container similar to the set, with the exception that multiple elements can have the same values. Union is In this article, we will see how to find the union of two multisets in C++ STL. Example Input: first_multiset = {100,300,400,500,200,500,200}, second
3 min read
How To Find the Difference of Two Multimaps in C++? In C++ STL, finding the difference between two multimaps consists of identifying the elements that exist in one multimap but are not present in the other. In this article, we will learn how to find the difference between two multimaps in C++ STL. Example:Input: multimap1 = {1, "Java"}, {2, "Python"}
2 min read
How to Find the Union of Two Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how to find the union of two sets in C++. Example Input:set1 = {12 , 13, 14
2 min read
How To Find the Intersection of Two Multimaps in C++? In C++, finding the intersection of two multimaps consists of identifying the common elements that are shared between both collections, containing key-value pairs. In this article, we will learn how to find the intersection of two multimaps in C++ STL. Example:Input:multimap1 = {1, "Java"}, {2, "Pyt
2 min read