![]() |
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_intersectionset_intersection (first1, last1, first2, last2, result);
Here,
C++ Program to Find the Intersection of Two SetsThe below example demonstrates how we can find the intersection of the given two sets in C++ STL. C++
Output
Intersection of two sets: 10 30 40 50 60 Time Complexity: O(N + M), where N and M are the sizes of the set. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |