Javatpoint Logo
Javatpoint Logo

C++ Algorithm set_symmetric_difference()

C++ Algorithm set_symmetric_difference() function is used to find the symmetric difference between two sorted ranges[first1, last1) and [first2, last2), which is formed by the elements and is present in one of the range, but not in the other.

Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version.

Syntax

Parameter

first1: An input iterator pointing to the first element in the first of two sorted source sequences.

last1: An input iterator pointing to the past last element in the first of two sorted source sequences.

first2: An input iterator pointing to the first element in the second sorted source sequence.

last2: An input iterator pointing to the past last element in the second sorted source sequence.

comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order otherwise returns false. It follows the strict weak ordering to order the elements.

result: An output iterator addressing to the position of the first element in the destination range.

Return value

This function returns an iterator to the end of the constructed range.

Complexity

Complexity is linear in the distance between [first1, last1) and [first2, last2): performs up to 2*(count1+count2)-1 comparisons. Where count1 = last1- first1 and count2 = last2- first2.

Data races

The object in the range [first1, last1) and [first2. last2) are accessed.

The object in the range between result and returned value are modified.

Exceptions

This function throws an exception if any of element comparisons, the element assignments or an operation on iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see a simple example:

Output:

1
5
8
7

Example 2

Let's see another simple example:

Output:

First array contains : 5 10 15 20 25
Second array contains : 50 40 30 20 10

The difference has 6 elements:
 5 15 25 30 40 50

Example 3

Let's see another simple example to find the students that are not taking both classes:

Output:

Students attending first class are : Nikita Deep Aman Divya 
Students attending second class are : Nikita Amita Deep Priya Kashish 

List of students that are not taking both classes :Amita Aman Divya Priya Kashish  

Example 4

Let's see another simple example:

Output:

A D E F

Next TopicC++ Algorithm





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA