Symmetric difference of two sorted ranges
The symmetric difference between two sets is formed by the elements that are present in one of the sets, but not in the other. Among the equivalent elements in each range, those discarded are those that appear before in the existent order before the call. The existing order is also preserved for the copied elements.
The elements are compared using operator< for the first version, and comp for the second. Two elements, a and b are considered equivalent if (!(a<b) && !(b<a)) or if (!comp(a, b) && !comp(b, a)).
The elements in the ranges shall already be ordered.
1. Using default operator< :
Syntax :
Template :
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
Parameters :
first1, last1
Input iterators to the initial and final positions of the first
sorted sequence. The range used is [first1, last1), which contains
all the elements between first1 and last1, including the element
pointed by first1 but not the element pointed by last1.
first2, last2
Input iterators to the initial and final positions of the second
sorted sequence. The range used is [first2, last2).
result
Output iterator to the initial position of the range where the
resulting sequence is stored.
The pointed type shall support being assigned the value of an
element from the other ranges.
comp
Binary function that accepts two arguments of the types pointed
by the input iterators, and returns a value convertible to bool.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
The ranges shall not overlap.
Return Type :
An iterator to the end of the constructed range.
CPP
#include <algorithm> // std::set_symmetric_difference, std::sort
#include <iostream> // std::cout
#include <vector> // std::vector
int main()
{
int first[] = { 5, 10, 15, 20, 25 };
int second[] = { 50, 40, 30, 20, 10 };
int n = sizeof (first) / sizeof (first[0]);
std::cout << "First array contains :" ;
for ( int i = 0; i < n; i++)
std::cout << " " << first[i];
std::cout << "\n" ;
std::cout << "Second array contains :" ;
for ( int i = 0; i < n; i++)
std::cout << " " << second[i];
std::cout << "\n\n" ;
std::vector< int > v(10);
std::vector< int >::iterator it, st;
std::sort(first, first + 5);
std::sort(second, second + 5);
it = std::set_symmetric_difference(first, first + 5,
second, second + 5, v.begin());
std::cout << "The symmetric difference has "
<< (it - v.begin()) << " elements:\n" ;
for (st = v.begin(); st != it; ++st)
std::cout << ' ' << *st;
std::cout << '\n' ;
return 0;
}
|
Output:
First array contains : 5 10 15 20 25
Second array contains : 50 40 30 20 10
The symmetric difference has 6 elements:
5 15 25 30 40 50
2. Using custom function :
Syntax :
Template :
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
Parameters :
first1, last1, first2, last2, result are same as described above.
comp
Binary function that accepts two arguments of the types pointed
by the input iterators, and returns a value convertible to bool.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
The ranges shall not overlap.
Return Type :
An iterator to the end of the constructed range.
CPP
#include <algorithm> // std::set_symmetric_difference, std::sort
#include <iostream> // std::cout
#include <vector> // std::vector
bool comp( int a, int b)
{
return a < b;
}
int main()
{
int first[] = { 5, 10, 15, 20, 25 };
int second[] = { 50, 40, 30, 20, 10 };
int n = sizeof (first) / sizeof (first[0]);
std::cout << "First array contains :" ;
for ( int i = 0; i < n; i++)
std::cout << " " << first[i];
std::cout << "\n" ;
std::cout << "Second array contains :" ;
for ( int i = 0; i < n; i++)
std::cout << " " << second[i];
std::cout << "\n\n" ;
std::vector< int > v(10);
std::vector< int >::iterator it, st;
std::sort(first, first + 5);
std::sort(second, second + 5);
it = std::set_symmetric_difference(first, first + 5,
second, second + 5, v.begin(), comp);
std::cout << "The symmetric difference has "
<< (it - v.begin()) << " elements:\n" ;
for (st = v.begin(); st != it; ++st)
std::cout << ' ' << *st;
std::cout << '\n' ;
return 0;
}
|
Output:
First array contains : 5 10 15 20 25
Second array contains : 50 40 30 20 10
The symmetric difference has 6 elements:
5 15 25 30 40 50
Possible Application: It is used to find the elements that are present in one container and not in another container.
1. It is used to find the list of students that are not taking both classes. Students of both classes are present in lists.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
std::vector<string> class1{ "Samir" , "Manoj" , "Pranav" , "Rajesh" };
std::vector<string> class2{ "Samir" , "Junaid" , "Manoj" , "Pankaj" , "Arpit" };
cout << "Students attending first class are : " ;
for ( auto i : class1) {
cout << i << " " ;
}
cout << "\nStudents attending second class are : " ;
for ( auto i : class2) {
cout << i << " " ;
}
std::vector<string> result(10);
std::vector<string>::iterator it;
it = set_symmetric_difference(class1.begin(),
class1.end(), class2.begin(), class2.end(), result.begin());
cout << "\n\nList of students that are not taking both classes :" ;
for (std::vector<string>::iterator i = result.begin(); i != it; i++) {
cout << *i << " " ;
}
return 0;
}
|
OUTPUT :
Students attending first class are : Samir Manoj Pranav Rajesh
Students attending second class are : Samir Junaid Manoj Pankaj Arpit
List of students that are not taking both classes :Junaid Pankaj Arpit Pranav Rajesh
2. It can also be used to find the numbers from both lists that are not present in both lists.
The program is given above.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Oct, 2021
Like Article
Save Article