std::not_equal_to in C++ with Examples
Last Updated :
22 Jun, 2021
The std::not_equal_to is a functional object class for non-equality comparison and binary function object class. It returns a boolean value depending upon the condition whether the two arguments are not equal or not.
Header File:
#include <functional.h>
Template Class:
template struct not_equal_to :
binary_function {
// Declaration of the
// not equal to 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::not_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):
- True: If a is not equals to b.
- False: If a is equals to b.
Below is the illustration of std::not_equal_to in C++:
Program 1:
CPP
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > v1 = { 50, 55, 60,
65, 70 };
vector< int > v2 = { 50, 55, 85,
65, 70 };
pair<vector< int >::iterator,
vector< int >::iterator>
pairs1;
pairs1 = mismatch(v1.begin(), v1.end(),
v2.begin(),
not_equal_to< int >());
cout << "The 1st match element"
<< " of 1st container : " ;
cout << *pairs1.first << endl;
cout << "The 1st match element "
<< "of 2nd container : " ;
cout << *pairs1.second << endl;
return 0;
}
|
Output:
The 1st match element of 1st container : 50
The 1st match element of 2nd container : 50
Program 2:
CPP
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
template < typename A, typename B,
typename U = not_equal_to< int > >
bool f(A a, B b, U u = U())
{
return u(a, b);
}
int main()
{
int X = 1, Y = 2;
cout << boolalpha;
cout << f(X, Y) << '\n' ;
X = -1, Y = -1;
cout << f(X, Y) << '\n' ;
return 0;
}
|
Reference: http://www.cplusplus.com/reference/functional/not_equal_to/
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...