How to Find Union of Two Multisets in C++?
Last Updated :
21 Feb, 2024
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_multiset = {700,600,1000,500,500,500,400}
Output:
union = {100, 200, 200, 300, 400, 500, 500, 500, 600, 700, 1000}
Finding Union of Two Multisets in C++
To calculate the union of two multisets in C++, the std::set_union() function is provided by STL which is used to find the union of two sorted ranges. If the elements from the second multiset that have an equivalent element in the first multiset are not copied to the resulting container (Output container).
Syntax of std::set_union()
set_union (first1, last1, first2, last2, result);
where,
- first1: Iterator to the beginning of the first range.
- last1: Iterator to the last of the first range.
- first2: Iterator to the beginning of the second range.
- last1: Iterator to the last of the second range.
- result: Iterator to the beginning of the resulant data container.
C++ Program to Find Union of Two Multisets
C++
// C++ program to find union of two multisets.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Create first_multiset with integer type
// and insert 7 elements into it
multiset<int> first_multiset;
first_multiset.insert(100);
first_multiset.insert(300);
first_multiset.insert(400);
first_multiset.insert(500);
first_multiset.insert(200);
first_multiset.insert(500);
first_multiset.insert(200);
// Create second_multiset with integer type
// and insert 7 elements into it
multiset<int> second_multiset;
second_multiset.insert(700);
second_multiset.insert(600);
second_multiset.insert(1000);
second_multiset.insert(500);
second_multiset.insert(500);
second_multiset.insert(500);
second_multiset.insert(400);
// Displaying the first_multiset
cout << "First multiset: \n";
multiset<int>::iterator itr1;
for (itr1 = first_multiset.begin();
itr1 != first_multiset.end(); ++itr1) {
cout << *itr1 << ", ";
}
cout << endl;
cout << endl;
// Displaying the second_multiset
cout << "Second multiset: \n";
multiset<int>::iterator itr2;
for (itr2 = second_multiset.begin();
itr2 != second_multiset.end(); ++itr2) {
cout << *itr2 << ", ";
}
cout << endl;
cout << endl;
// Create vector to store the union of two multisets
// with size of both the multisets
vector<int> v(first_multiset.size()
+ second_multiset.size());
vector<int>::iterator it, st;
// Union of two multisets
it = set_union(first_multiset.begin(),
first_multiset.end(),
second_multiset.begin(),
second_multiset.end(), v.begin());
// Display vector
cout << "Union of the above multisets: \n";
for (st = v.begin(); st != it; ++st)
cout << *st << ", ";
return 0;
}
OutputFirst multiset:
100, 200, 200, 300, 400, 500, 500,
Second multiset:
400, 500, 500, 500, 600, 700, 1000,
Union of the above multisets:
100, 200, 200, 300, 400, 500, 500, 500, 600, 700, 1000,
Time complexity:O(N+M), where N and M is the size of the two multisets.
Auxiliary Space: O(N+M)
Similar Reads
How to Find the Union of Two Multimaps in C++? 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 = {
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 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 Intersection 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. Intersection means the elements that are common between two datasets. In this article, we will see how to find the intersection of two Multisets in C++. Examp
3 min read
How to Find Union of Two Vectors in C++? The union of two vectors refers to the collection of all the distinct elements from both vectors. In this article, we will explore different methods to find the union of two vectors in C++The most efficient way to find the union of two sorted vectors is by storing all elements of the two vectors int
3 min read
How to Find the Union of Two Deques in C++? In C++, deques also called double-ended queues are sequence containers that can expand and contract on both ends. The union of two deques means the set of elements that consists of all the elements of the two deques at once. In this article, we will learn how to find the union of two deques in C++.
3 min read