Difference between Two Sets in C++
- The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the first range's contents in the same order. The elements of both ranges will already be assembled.
Comparing items using "<"
- As for the syntax:
- The structure:
- OutputIterator result: Set difference (InputIterator1 first, InputIterator1 last, InputIterator2 first, InputIterator2 last);
The variables:
- Iterators for the first and last locations of the input's first sorted sequence are first1, last1, respectively.
- All items between first1 and last1 are included in the range [first1, last1], even the element that is pointed by first1 but not by last1.
- Iterators for the first and last places of the second sorted sequence are first2 and last2.
- The utilised range is [first2, last2].
- result Iterator to the location in the range where the result is recorded as a sequence.
- An element from the first range should be able to have its value set to it using the pointed type.
Format of Return:
- An iterator that advances you to the range's limit.
- The items that are present exclusively in the first list and not in the second list are sought after. 1. The list of students who are just attending the initial classes may be found using it.
// CPP program to illustrate
// std :: set_difference
#include <bits/stdc++.h>
int main()
{
int first[] = { 1,2,3,4,5 };
int second[] = { 55,44,33,22,11 };
int n = sizeof(first) / sizeof(first[0]);
std::vector<int> v2(5);
std::vector<int>::iterator it, ls;
std::sort(first, first + 5);
std::sort(second, second + 5);
// Print elements
std::cout<< "First array :";
for (int i = 0; i< n; i++)
std::cout<< " " << first[i];
std::cout<< "\n";
// Print elements
std::cout<< "Second array :";
for (int i = 0; i< n; i++)
std::cout<< " " << second[i];
std::cout<< "\n\n";
// using default comparison
/* first array intersection second array */
ls = std::set_difference(first, first + 5, second, second + 5, v2.begin());
std::cout<< "Using default comparison, \n";
std::cout<< "The difference has " << (ls - v2.begin()) << " elements :";
for (it = v2.begin(); it < ls; ++it)
std::cout<< " " << *it;
std::cout<< "\n";
return 0;
}
Output:
First array : 1 2 3 4 5
Second array :11 22 33 44 55
Using custom comparison :
The difference has 3 elements : 3 4 5
Comparing using a function that has already been defined Template:
- OutputIterator set difference between the first two input iterators, the last two input iterations, OutputIterator output, and comparative analysis;
Parameters:
- The outcomes of first1, last1, first2, and last2 are identical.
- Comp binary function that returns a value that can be converted to a bool from the input iterators and takes two pointed types as inputs.
- The function can't have any parameters changed.
- This could be a function pointer or an object.
- Submitted by:an iterator to the built-in range's limit.
// CPP program to illustrate
// std :: set_difference
#include <bits/stdc++.h>
bool comp(int i, int j)
{
return (i< j); }
int main()
{
int first[] = {55,44,33,22,11 };
int second[] = { 1,2,3,4,5};
int n = sizeof(first) / sizeof(first[0]);
std::vector<int> v1(5);
std::sort(first, first + 5);
std::sort(second, second + 5);
std::cout<< "First array :";
for (int i = 0; i< n; i++)
std::cout<< " " << first[i];
std::cout<< "\n";
std::cout<< "Second array :";
for (int i = 0; i< n; i++)
std::cout<< " " << second[i];
std::cout<< "\n\n";
// using custom comparison, function as comp
/* first array intersection second array */
ls = std::set_difference(second, second + 5, first, first + 5, v1.begin(), comp);
std::cout<< "Using custom comparison, \n";
std::cout<< "The difference has " << (ls - v1.begin()) << " elements :";
for (it = v1.begin(); it < ls; ++it)
std::cout<< " " << *it;
std::cout<< "\n";
return 0; }
Output:
First array : 11 22 33 44 55
Second array : 5 10 15 20 25
Using custom comparison :
The difference has 3 elements : 3 4 5