C++ boost::range::set_symmetric_difference














































C++ boost::range::set_symmetric_difference



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, " "};
   
// Set symmetric difference based on default operator<
    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: ";
   
// Set symmetric  difference based on default operator<
    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.

More Articles of Abhishek Kumar Singh:

Name Views Likes
C++ boost::range::replace_if 1068 0
C++ boost::range::copy_backward 806 1
C++ boost::range::max_element 1283 0
C++ boost::range::inplace_merge 736 0
C++ boost::range::copy 1525 1
C++ boost::algorithm::is_partitioned() 910 1
C++ boost::algorithm::copy_if() 1137 0
C++ boost::range::for_each (version 2) 807 0
C++ boost::range::set_symmetric_difference 728 0
C++ boost::remove_copy_if 816 0
C++ boost::range::set_intersection 1126 0
C++ boost::range::find_end 752 0
C++ boost::range::remove_erase_if 1807 0
C++ boost::range::push_back 1255 0
C++ boost::range::generate 711 0
C++ boost::algorithm::any_of() 820 0
C++ boost::range::insert 827 0
C++ boost::range::remove_erase 992 0
C++ boost::range::reverse 1331 0
C++ boost::algorithm::equal() 834 1
C++ boost::range::copy_n 898 0
C++ boost::range::random_shuffle 1356 1
C++ boost::algorithm::partition_point() 697 0
C++ boost::algorithm::one_of_equal() 575 0
C++ boost::algorithm::all_of() 1013 1
C++ boost::range::merge 1144 0
C++ boost::range::reverse_copy 810 0
C++ boost::range::find 899 0
C++ boost::range::fill_n 659 0
Removing duplicate elements from std::vector (using std::unique and std::set) 2891 1
C++ boost::range::equal 780 0
C++ boost::algorithm::iota() 1162 0
C++ boost::range::is_sorted 884 0
test article 845 2
C++ boost::algorithm::is_permutation() 940 1
C++ boost::partial_sum 936 0
C++ boost::range::partial_sort 981 1
C++ boost::range::min_element 1139 0
C++ boost::range::iota 1152 0
C++ boost::range::set_union 796 0
C++ boost::algorithm::partition_copy() 1143 3
C++ boost::range::swap_ranges 688 0
C++ boost::range::for_each 988 0
C++ boost::range::upper_bound 1153 0
C++ boost::range::binary_search 1313 0
C++ boost::algorithm::all_of_equal() 595 0
C++ boost::algorithm::copy_n() 729 1
C++ boost::range::lower_bound 1110 0
C++ boost::algorithm::gather() 1658 0
C++ boost::algorithm::none_of_equal() 638 0
C++ boost::algorithm::one_of() 771 2
C++ boost::range::rotate 921 0
C++ boost::algorithm::any_of_equal() 944 0
Use of Comparator in C++ 2244 0
C++ boost::range::count 869 0
C++ boost::range::replace_copy_if 673 0
C++ boost::range::remove 891 2
C++ boost::remove_if 1577 1
C++ boost::range::nth_element 1209 1
C++ boost::range::partition 800 1
C++ boost::range::erase 721 0
C++ boost::range::fill 941 1
C++ boost::range::find_if 1474 0
C++ boost::range::lexicographical_compare 848 0
C++ boost::algorithm::none_of() 633 0
C++ boost::algorithm::hex() 4220 0
C++ boost::range::replace 820 0
C++ boost::range::replace_copy 857 1
C++ boost::range::set_difference 1095 0
C++ boost::range::overwrite 775 0
C++ boost::range::count_if 1024 0
C++ boost::range::push_front 707 0
C++ boost::range::includes 705 0
C++ boost::algorithm::is_sorted() 742 0
C++ boost::range::remove_copy 663 1
C++ boost::algorithm::minmax_element 656 1
Deletion of leaf node of a Binary Search Tree 1820 1
Nth Fibonacci Number (Recursive Solution, Dynamic Programming, Iterative Solution 4083 1
C++ boost::range::rotate_copy 699 0

Comments