How to Find Intersection 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. 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++.
Example
Input:
myMultiset2 = {100,300,400,500,200,500,200},
myMultiset2 = {700,600,1000,500,500,500,400}
Output:
{400, 500, 500}
Finding Intersection of Two Multisets in C++
To find the intersection of two multisets in C++, we can use the std::set_intersection() function provided by STL which finds the intersection of two sorted ranges and saves the result in the given container.
Syntax of std::set_intersection()
set_intersection (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 the Intersection of Two Multisets
C++
// CPP Code to find the intersection of two multisets
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
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 intersection of two
// multisets with size of both the multisets
vector<int> v(first_multiset.size()
+ second_multiset.size());
vector<int>::iterator it, st;
// Intersection of two multisets
it = set_intersection(first_multiset.begin(),
first_multiset.end(),
second_multiset.begin(),
second_multiset.end(), v.begin());
// Display vector
cout << "Intersection of the above multisets: \n";
for (st = v.begin(); st != it; ++st)
cout << *st << ", ";
return 0;
}
[tabbyending]
OutputFirst multiset:
100, 200, 200, 300, 400, 500, 500,
Second multiset:
400, 500, 500, 500, 600, 700, 1000,
Intersection of the above multisets:
400, 500, 500,
Time complexity :O(N+M), where N and M is the size of first and second multiset.
Auxiliary Space: O(N+M)
Similar Reads
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
How to Find Intersection of Two Sets in C++? In C++, sets are containers that store unique elements following a specific order. The intersection of two datasets includes all the elements that are common in both sets. In this article, we will learn how to find the intersection of two sets in C++ STL. Example:Input: mySet1 = {10,20,30,40,50,60}
2 min read
How to Find the Intersection of Two Maps in C++? In C++, a map is a container that stores elements in the form of key and value pairs. Intersection means the elements that are common between two datasets. In this article, we will learn how to find the intersection of two maps in C++ STL. Example: Input: map1 = {{âappleâ, 1}, {âbananaâ, 2}, {âcherr
3 min read
How to Find the Intersection of Two Vectors in C++? Intersection of two vectors is the set of common elements in both vectors. In this article, we will learn how to find the intersection of two vectors in C++.The most efficient method to find the intersection of two vector is by using set_intersection() function. Letâs take a look at an example:C++#i
3 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 Intersection of Two Deques in C++? In C++, a deque is a sequence container that allows insertions and deletions at both its ends i.e., the beginning and the end, and the intersection of two deques includes all the common elements in both deques. In this article, we will learn how to find the intersection of two deques in C++. Example
2 min read