How to Find the Intersection of a Vector and a Set in C++?
Last Updated :
15 Feb, 2024
In C++, vectors and sets are two data containers that can store a collection of elements. In this article, we will learn how to find the intersection of a vector and a set in C++.
Example
Input:
myVector = { 1, 4, 2, 3, 7, 6, 5}
myset = { 1, 2, 3, 5, 7, 11, 12 }Output:
Intersection = {1, 2, 3, 4, 5, 7}
Find the Intersection of a Vector and a Set in C++
Intersection represents the common elements or values present in both data structures. To find the intersection of a vector and a set, we can use the std::set_intersection method that finds the intersection between two sorted ranges, adds the data in the given container, and returns the iterator to the last.
Syntax of set_intersection()
set_intersection (first1, first2, last1, last2, result);
C++ Program to Find the Intersection of a Vector and a Set
Here we will see the implementation for finding intersection of a vector and set.
C++
// C++ Program to Find Common Elements or intersection
// Between a Vector and a Set
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main()
{
// Define two containers: a vector and a set
vector<int> myVector = { 1, 4, 2, 3, 7, 6, 5 };
set<int> mySet = { 1, 2, 3, 5, 7, 11, 12 };
sort(myVector.begin(), myVector.end());
// Initialize a vector to store common elements
vector<int> common_elements;
// Find the common elements between vec1 and mySet
set_intersection(myVector.begin(), myVector.end(),
mySet.begin(), mySet.end(),
back_inserter(common_elements));
// Print the common elements
cout << "Common elements: ";
for (int elem : common_elements)
cout << elem << " ";
cout << endl;
return 0;
}
OutputCommon elements: 1 2 3 5 7
Time Complexity: O(MlogM+NlogN) where M is the size of the vector and N is the size of the set./
Space Complexity: O(min(M,N))
Similar Reads
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 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 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 Size of a Vector in Bytes in C++? In C++, Vectors are dynamic containers that can change their size during the insertion or deletion of elements. In this article, we will explore how we can find the size of the vector in bytes in C++. Example: Input:myVector = {10,20,30,40,50}Output:Size of the vector in bytes is : 20 bytesFind the
2 min read
How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s
2 min read