Description:
The header 'boost/range/algorithm/set_algorithm.hpp' contains the algorithm set_symmetric_difference() in C++ boost library. This function constructs a sorted range that is the set symmetric difference of rng1 and rng2. The set symmetric difference of two sets is formed by removing all the elements that are present in both the sets i.e. the elements that are present in one of the sets, but not in the other. The ordering relationship is determined by using operator< in non-predicate versions, and by evaluating predicate pred in the predicate versions.
Syntax:template<class SinglePassRange1, class SinglePassRange2, class OutputIterator>
OutputIterator set_symmetric_difference(const SinglePassRange1& rng1, const SinglePassRange2& rng2, OutputIterator out);
template<class SinglePassRange1, class SinglePassRange2, class OutputIterator, class BinaryPredicate>
OutputIterator set_symmetric_difference(const SinglePassRange1& rng1, const SinglePassRange2& rng2, OutputIterator out, BinaryPredicate pred);
Parameters:
The following parameters are accepted by the function:
1.) rng1: It specifies the first sorted sequence.
2.) rng2: It specifies the second sorted sequence.
3.) out: It specifies the initial position of the destination range.
4.) pred: Binary predicate which accepts two elements of the sequence and returns the boolean value.
Program:
#include <boost/range/algorithm/set_algorithm.hpp>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> Set1 = {1, 2, 3, 4, 5}, Set2 = {4, 5, 6, 7};
cout << "Content of first set: ";
for(auto i : Set1)
cout << i << " ";
cout << endl << "Content of second set: ";
for(auto i : Set2)
cout << i << " ";
cout << endl << "Set symmetric difference of first and second set is: ";
ostream_iterator<int> out {cout, " "};
boost::range::set_symmetric_difference(Set1, Set2, out);
string str1 = "abcdez", str2 = "bdexyz", final_str;
cout << "\n\nContent of first string: " << str1 <<endl;
cout << "Content of second string: " <<str2 << endl;
cout << "Set symmetric difference of both string is: ";
boost::range::set_symmetric_difference(str1, str2, back_inserter(final_str));
cout << final_str;
}
Output:
Content of first set: 1 2 3 4 5
Content of second set: 4 5 6 7
Set symmetric difference of first and second set is: 1 2 3 6 7
Content of first string: abcdez
Content of second string: bdexyz
Set symmetric difference of both string is: acxy
Complexity:
Time Complexity: The time complexity is linear i.e. O(N) where N is distance(rng1) + distance(rng2).
Please write on comment section if you find any mistake or for any suggestions.
Comments