Open In App

How to Find Symmetric Difference of Two Multimaps in C++?

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 of two multimaps in C++.

For Example,

Input: 
mp1 = {{"apple", 1}, {"banana", 2}, {"cherry", 3}};
mp2 = {{"banana", 3}, {"cherry", 3}, {"date", 4}};
Output:
Symmetric Difference:
apple 1
banana 2
banana 3
date 4


Symmetric Difference of Two Multimaps

To find the symmetric difference between two multimaps in C++, we can use the std::set_symmetric_difference() function that finds the symmetric difference between two sorted ranges.

Syntax of std::set_symmetric_difference

set_symmetric_difference(first1, last1, first2, last2, result);

Here,

  • 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 Multimaps

The below example demonstrates how we can use the set_symmetric_difference to find the symmetric difference of two multimaps in C++ STL.

C++
// C++ Program to illustrate how to find the symmetric
// difference of two multimaps
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main()
{
    // Define two multimap containers
    multimap<string, int> map1 = { { "apple", 1 },
                                   { "banana", 2 },
                                   { "cherry", 3 } };
    multimap<string, int> map2 = { { "banana", 3 },
                                   { "cherry", 3 },
                                   { "date", 4 } };

    // Create a vector to store the symmetric difference of
    // the two maps
    vector<pair<string, int> > sym_diff;

    // Find the symmetric difference between map1 and map2
    // and store it in sym_diff
    set_symmetric_difference(map1.begin(), map1.end(),
                             map2.begin(), map2.end(),
                             back_inserter(sym_diff));

    // Iterate through the symmetric difference vector and
    // print the key-value pairs
    cout << "Symmetric Difference:" << endl;
    for (auto it = sym_diff.begin(); it != sym_diff.end();
         ++it) {
        cout << it->first << " " << it->second << endl;
    }

    return 0;
}

Output
Symmetric Difference:
apple 1
banana 2
banana 3
date 4

Time Complexity: O(N * log(M)), where N is the size of one string
Space Complexity: O(N + M), where M is average size of the string.




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads