How to Find the Intersection of Two Maps in C++?
Last Updated :
28 Feb, 2024
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}, {“cherry”, 3}};
map2 = {{“banana”, 2}, {“cherry”, 3}, {“date”, 4}};
Output:
Intersection= {{“banana”, 2}, {“cherry”, 3}}
Finding the Intersection of Two Maps in C++
There is no direct function to find the intersection of two maps in C++. But we can create a custom function that compares the key and values of pairs of two maps and if they match, it stores them to another map. It keeps doing this till all the elements of both the maps are scanned.
Approach
- Create a template function that takes two maps as input and returns a new map which is the intersection of the input maps.
- Inside this function, create an empty map that will store the intersection.
- Start by iterating the elements of the first map.
- For each key-value pair, check if the key exists in the second map.
- If the key exists in both maps, compare the value.
- If the value is same, add this pair to the intersection map.
- Keep doing that till all the elements of first map is scanned.
C++ Program to Find the Intersection of Two Maps
C++
// C++ Program to illustrate how to Find the Intersection of
// Two Maps
#include <iostream>
#include <map>
using namespace std;
// Template function to find the intersection of two maps
template <typename K, typename V>
map<K, V> find_intersection(const map<K, V>& map1,
const map<K, V>& map2)
{
// Create an empty map to store the intersection
map<K, V> intersection;
// Iterate through each key-value pair in the first map
for (const auto& pair : map1) {
K key = pair.first;
V value = pair.second;
// Check if the key exists in the second map
if (map2.count(key) > 0) {
// If the key exists, add the key-value pair to
// the intersection map
intersection[key] = value;
}
}
// Return the map containing the intersection elements
return intersection;
}
int main()
{
// Create two sample maps
map<int, string> map1 = { { 1, "apple" },
{ 2, "banana" },
{ 3, "cherry" } };
map<int, string> map2 = { { 2, "banana" },
{ 4, "grape" },
{ 3, "orange" } };
// Find the intersection of the maps
map<int, string> intersection
= find_intersection(map1, map2);
// Print the key-value pairs in the intersection map
for (const auto& pair : intersection) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Output2: banana
3: cherry
Time Complexity: O(N), where N is the number of elements of the first map.
Auxiliary Space: O(K) where K is the number of elements in the intersection vector.
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 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 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 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 a Vector and a Set in C++? 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
2 min read