std::equal_to in C++ with Examples

The std::equal_to allows the equality comparison to be used as a function, which means that it can be passed as an argument to templates and functions. This is not possible with the equality operator == since operators cannot be passed as parameters.

Header File:

#include <functional.h>

Template Class:

template struct equal_to : binary_function
{
 
  // Declaration of the equal operation
  bool operator() (const T& x,
                   const T& y) 
       const 
  { 
     return x==y;
  }

  // Type of first parameter
  typedef T first_argument_type;

  // Type of second parameter
  typedef T second_argument_type;

  // The result is returned
  // as bool type
  typedef bool result_type;
}

Syntax:

std::equal_to <int> ()

Parameter: This function accepts the type of the arguments T, as the parameter, to be compared by the functional call.



Return Type: It return a boolean value depending upon condition(let a & b are 2 element):

Below is the illustration of std::equal_to in C++:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to illustrate std::equal_to
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
  
// Driver Code
int main()
{
    // Intialise vectors
    vector<int> v1 = { 50, 55, 60, 
                       65, 70 };
    vector<int> v2 = { 50, 55, 85, 
                       65, 70 };
  
    // Declaring pointer of pairs
    pair<vector<int>::iterator,
         vector<int>::iterator>
        pairs1;
  
    // Use mismatch() function to
    // search first mismatch between
    // v1 and v2
    pairs1 = mismatch(v1.begin(), v1.end(),
                      v2.begin(),
                      equal_to<int>());
  
    // Print the mismatch pair
    cout << "The 1st mismatch element"
         << " of 1st container : ";
    cout << *pairs1.first << endl;
  
    cout << "The 1st mismatch element"
         << " of 2nd container : ";
    cout << *pairs1.second << endl;
  
    return 0;
}
chevron_right

Output:
The 1st mismatch element of 1st container : 60
The 1st mismatch element of 2nd container : 85

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate std::equals_to
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
  
// Template
template <typename A, typename B, 
          typename U = std::equal_to<int> >
  
// Function to check if a = b or not
bool f(A a, B b, U u = U())
{
    return u(a, b);
}
  
// Driver Code
int main()
{
    int X = 1, Y = 2;
  
    // If X is equals to Y or not
    cout << std::boolalpha;
    cout << f(X, Y) << '\n';
  
    X = -1, Y = -1;
  
    // If X is equals to Y or not
    cout << f(X, Y) << '\n';
  
    return 0;
}
chevron_right

Output:
false
true

Reference: http://www.cplusplus.com/reference/functional/equal_to/

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :