I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlAlgorithmsSet

set_symmetric_difference

Processes a sorted range that contains all elements that are in exactly one of two ranges
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The set_symmetric_difference() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class InputIterator1, class InputIterator2, class OutputIterator >
   OutputIterator set_symmetric_difference(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2, 
      InputIterator2 last2, 
      OutputIterator result
   );
template < class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate >
   OutputIterator set_symmetric_difference(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2, 
      InputIterator2 last2, 
      OutputIterator result,
      BinaryPredicate comp
   );

Parameters:
Parameter Description
first1 An input iterator addressing the position of the first element in the first of two sorted source ranges to be united and sorted into a single range representing the symmetric difference of the two source ranges
last1 An input iterator addressing the position one past the last element in the first of two sorted source ranges to be united and sorted into a single range representing the symmetric difference of the two source ranges
first2 An input iterator addressing the position of the first element in second of two consecutive sorted source ranges to be united and sorted into a single range representing the symmetric difference of the two source ranges
last2 An input iterator addressing the position one past the last element in second of two consecutive sorted source ranges to be united and sorted into a single range representing the symmetric difference of the two source ranges
result An output iterator addressing the position of the first element in the destination range where the two source ranges are to be united into a single sorted range representing the symmetric difference of the two source ranges
comp User-defined predicate function object that defines the sense in which one element is greater than another. The binary predicate takes two arguments and should return true when the first element is less than the second element and false otherwise

Description

Set_symmetric_difference copies the elements from the sorted range [first1, last1) which are not found in the sorted range [first2, last2) and the elements from the sorted range [first2, last2) which are not found in the sorted range [first1, last1) to the range beginning at result.
If a value appears m times in [first1, last1) and n times in [first2, last2) (where m or n may be zero), then it appears abs(m-n) times in the output range.
The operation is stable, meaning that the relative order of elements within each input range is preserved.

The first version compares objects using operator<, and the second compares objects using a function object comp.

Return Value

Returns an iterator on the element following the last of the build sequence.

Complexity

The complexity is linear; performs at most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons.
Example:
Example - set_symmetric_difference algorithm
Problem
This program illustrates the use of the STL set_symmetric_difference() algorithm (default version) to find the values that are in a first vector of integers but not in a second vector of integers, as well as those integers that are in the second vector but not in the first, and write them out to a third vector of integers.
Workings
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a1[] = {11, 12, 12, 12, 12, 13, 14, 15};
  vector<int> v1(a1, a1+8);
 
  cout <<"\nHere are the values in the vector v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  int a2[] = {11, 12, 12, 13, 13, 16, 17, 18};
  vector<int> v2(a2, a2+8);
 
  cout <<"\nHere are the values in the vector v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";
 
  int a3[] = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115};
  vector<int> v3(a3, a3+15);
 
  cout <<"\nHere are the values in the vector v3:\n";
  for (vector<int>::size_type i=0; i<v3.size(); i++)
    cout <<v3.at(i)<<" ";
 
   cout <<"\nNow we find the values that are in v1 but not in v2, "
          "as well as those that are\nin v2 but not in v1, and write "
          "them out to v3, starting at the beginning of v3."
 
  vector<int>::iterator p;
  p = set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
 
  cout <<"\nHere are the revised contents of v3:\n";
  for (vector<int>::size_type i=0; i<v3.size(); i++)
    cout <<v3.at(i)<<" ";
 
  cout <<"\nThe iterator returned by the algorithm is pointing at the value "<<*p<<".";
 
  return 0;
}
Solution
Output:

Here are the values in the vector v1:
11 12 12 12 12 13 14 15

Here are the values in the vector v2:
11 12 12 13 13 16 17 18

Here are the values in the vector v3:
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

Now we find the values that are in v1 but not in v2, as well as those that are
in v2 but not in v1, and write them out to v3, starting at the beginning of v3.

Here are the revised contents of v3:
12 12 13 14 15 16 17 18 109 110 111 112 113 114 115

The iterator returned by the algorithm is pointing at the value 109.
References