How to Find Intersection of Two Sets in C++?
Last Updated :
04 Mar, 2024
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}
mySet2 = {10,30,50,40,60,80}
Output:
Intersection of two sets: 10 30 40 50 60
Find the Intersection of Two STL Sets in C++
To find the intersection of two sets in C++, we can use the std::set_intersection() function provided in the STL <algorithm> library that is used to find the intersection of two sorted ranges and then use the inserter to insert the result after the intersection in a new set.
Syntax of std::set_intersection
set_intersection (first1, last1, first2, last2, result);
Here,
- 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.
- last2: Iterator to the last of the second range.
- result: Iterator to the beginning of the resultant data container.
C++ Program to Find the Intersection of Two Sets
The below example demonstrates how we can find the intersection of the given two sets in C++ STL.
C++
// C++ Program to illustrate how to find the intersection of
// two sets
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main()
{
// creating two sets
set<int> set1 = { 10, 20, 30, 40, 50, 60 };
set<int> set2 = { 10, 30, 50, 40, 60, 80 };
// Declare a set to store the result of the intersection
set<int> result;
// Call the set_intersection(), which computes the
// intersection of set1 and set2 and
// inserts the result into the 'result' set
set_intersection(set1.begin(), set1.end(), set2.begin(),
set2.end(),
inserter(result, result.begin()));
// Print the result
cout << "Intersection of two sets: ";
for (int i : result)
cout << i << " ";
return 0;
}
OutputIntersection of two sets: 10 30 40 50 60
Time Complexity: O(N + M), where N and M are the sizes of the set.
Auxilliary Space: O(N + M)
Similar Reads
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 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 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
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 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