How to Find Symmetric Difference of Two Maps in C++?
Last Updated :
29 Feb, 2024
In C++, a map is a container that stores elements in a mapped fashion. Each element has a key value and a mapped value. The symmetric difference between two sets is formed by the elements that are present in one of the sets, but not in the other. In this article, we will learn how to find the symmetric difference of two maps in C++.
Example:
Input:
map1 = {{“apple”, 1}, {“banana”, 2}, {“cherry”, 3}};
map2 = {{“banana”, 2}, {“cherry”, 3}, {“date”, 4}};
Output:
Symmetric_difference: {{“apple”, 1}, {“date”, 4}}
Finding Symmetric Difference of Two Maps in C++
To find the symmetric difference of two std::maps, we can use the std::set_symmetric_difference() function from the <algorithm> library. This function constructs a sorted range beginning at d_first consisting of elements that are found in either of the sorted ranges [first1, last1) and [first2, last2).
Syntax of set_symetric_difference()
set_symmetric_difference (first1,last1, first2, last2, result);
where,
- first1, last1: Input iterators to the initial and final positions of the first sorted sequence.
- first2, last2:Input iterators to the initial and final positions of the second sorted sequence.
- result Output iterator to the initial position of the range where the resulting sequence is stored.
C++ Program to Find the Symmetric Difference of Two Maps
C++
// C++ Program to find the symmetric difference of two maps
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
// Creating two maps of string and int
map<string, int> map1 = { { "apple", 1 },
{ "banana", 2 },
{ "cherry", 3 } };
map<string, int> map2 = { { "banana", 2 },
{ "cherry", 3 },
{ "date", 4 } };
// Declaring a vector to store the symmetric difference
vector<pair<string, int> > sym_diff;
// Finding the symmetric difference of the two maps
set_symmetric_difference(map1.begin(), map1.end(),
map2.begin(), map2.end(),
back_inserter(sym_diff));
// Displaying the symmetric difference of the two maps
for (auto it = sym_diff.begin(); it != sym_diff.end();
++it) {
cout << it->first << " " << it->second << endl;
}
return 0;
}
Time Complexity: O(N + M), where N and M are the sizes of the two input maps.
Auxiliary Space: O(N + M)
Similar Reads
How to Find Symmetric Difference of Two Multimaps in C++? In C++, a multimap is a container that contains a sorted list of key-value pairs and there can be multiple entries having the same key. The symmetric difference between two multimaps gives all the unique elements from both multimaps. In this article, we will see how to find the symmetric difference
2 min read
How to Find the Difference of Two Maps in C++? In C++, a map is a container that stores key-value pairs in an ordered or sorted manner. Finding the difference between two maps involves determining the elements that are present in one map but not in the other. In this article, we will learn how to find the difference between two Maps in C++. Exam
2 min read
How to Find the Symmetric Difference of Two Deques in C++? In C++, the symmetric difference between two deques is a set of all elements present in either of the deques but not in their intersection. In this article, we will learn how to find the symmetric difference of two deques in C++. Example: Input: dq1 ={1,2,3,4,5} dq2 ={3,4,5,6,7} Output: Symmetric Di
2 min read
How To Find the Difference of Two Multimaps in C++? In C++ STL, finding the difference between two multimaps consists of identifying the elements that exist in one multimap but are not present in the other. In this article, we will learn how to find the difference between two multimaps in C++ STL. Example:Input: multimap1 = {1, "Java"}, {2, "Python"}
2 min read
How to Find the Difference of Two Sets in C++? In C++, the set container provides an efficient way to store unique elements in sorted order. Finding the difference between two sets involves determining the elements that are present in one set but not in the other. In this article, we are going to learn how to find the difference of two sets in C
2 min read