Horje
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 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.






Reffered: https://www.geeksforgeeks.org


C++

Related
How to Create a 2D Array of a Specific Size and Value in C++? How to Create a 2D Array of a Specific Size and Value in C++?
How to Create a Unordered Multimap of Tuples in C++? How to Create a Unordered Multimap of Tuples in C++?
How to Find the Union of Two Multimaps in C++? How to Find the Union of Two Multimaps in C++?
How to Find the Median of a Sorted Array in C++? How to Find the Median of a Sorted Array in C++?
How to Resize a Vector Without Initializing New Elements in C++? How to Resize a Vector Without Initializing New Elements in C++?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13