The std::unarynegate is a wrapper function object. It returns the opposite of the unary condition it contains. A wrapper function is a procedure in a software library or computer program that is designed to invoke a second subroutine or a system call with little or no extra processing. The function std::not1 is commonly used to create objects of type unarynegate.
Header Text:
#include<functional>
Syntax:
It has the following syntax:
std::unary_negate<class T>
var_name(Object of class T);
Parameters: The method std::unary_negate takes a predicate function object as an input and returns the logical counterpart of the result produced by invoking the predicate function.
Return Value: When the predicate function is called, it returns the logical complement of the result.
The following code displays the method std::unary_negate:
Filename: UnaryNegate.cpp
// C++ program to show the use of std::unary_negate
//to find a value larger than or equal to 6 in the array[]
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//Predicate method for finding the number higher than
//or equal to 4 in arrays array[]
struct con : unary_function<int, bool> {
//The function checks the number less than 6, and if present, returns true
bool operator()(int i)
const
{
return i < 6;
}
};
int main()
{
// vector declaration
vector<int> array;
// inserting values in the vector
for (int i = 1; i <= 10; ++i) {
array.push_back(i);
}
// To obtain the count of numbers bigger than 6, use unary_negate().
unary_negate<con> func((con()));
//Using the method count_if(), print the value of the count.
cout << count_if(array.begin(),
array.end(), func);
return 0;
}
Output:
Explanation:
The given array contains numbers from 1 to 10. From the array, the numbers that are greater than or equal to 6 are 6,7,8,9,10, so the count is 5.
Example 2:
// Program to find the number of even numbers in the array
#include < greater algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
// the struct form
struct IsOdd_cla {
// finding the predicate value of the structure
bool operator()(const int& x1)
const
{
return x1 % 2 == 1;
}
typedef int argument_type;
} IsOdd_obj;
// main
int main()
{
// Apply the unary_negate function
///to obtain the complement of a predicate declared in the structure
// IsOdd_class.
unary_negate<IsOdd_cla>
IsEven_obj(
IsOdd_obj);
// an array of elements to check the condition
int array[] = { 1, 1, 6, 9, 5};
int c;
// finding according to the predicate value
c = count_if(array, array + 5, IsEven_obj);
// Print the count
cout << "There were "
<< c
<< ?element, which was even!"
<< endl;
return 0;
}
Output:
There were 1 element, which was even!
Explanation:
The program constructs an IsOdd_cla structure that serves as a functor (a function object). It provides the operator method, which determines if a given integer is odd (x1% 2 == 1). This predicate is used to distinguish between odd and even integers.
The unarynegate function is used to generate a new predicate, which is the inverse of the IsOddobj predicate. It serves the purpose of finding even integers by skipping the check for odd numbers.
Advantages of the Unary_negate:
There are several advantages of the unarynegate in C++. Some main advantages of the unarynegate function in C++ are as follows:
- Code readability: It improves code readability and simplicity by creating the negation of a unary predicate. It eliminates the need to create a separate function for negation directly, making the code more concise and clear.
- Redundant Functor Definitions Should Be Avoided: Instead of establishing a new or changing an existing functor to represent a negation, std::unary_negate generates a negated version automatically on the run, avoiding the need for additional code.
- Algorithm Usage Simplicity: It combines smoothly with algorithms such as std::countif, std::removeif , and others, allowing for the use of negated predicates within these algorithms without the need to create additional function classes.