×

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

Related Topics

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.