×

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++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 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.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.