The std::unarynegate function is a wrapping function object that reverses the unary condition it encloses. A wrapping function is a routine within a software library or program that is created to call another subroutine or system operation with minimal additional processing. The std::not1 function is frequently employed to generate instances of the unarynegate type.
Header Text:
#include<functional>
Syntax:
It has the following syntax:
std::unary_negate<class T>
var_name(Object of class T);
The std::unary_negate function accepts a predicate function object as an argument and provides the inverse logical outcome of calling the predicate function.
When the predicate function is invoked, it yields the logical opposite of the output.
The provided code showcases the function 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 provided array consists of integers ranging from 1 to 10. Within this array, the elements that are equal to or exceed 6 are 6, 7, 8, 9, and 10, resulting in a total count of 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 software creates an IsOdd_cla construct that acts as a functor, serving as a function object. It includes the operator function, responsible for checking if a specified integer is odd (x1% 2 == 1). This condition helps differentiate odd numbers from even numbers.
The unarynegate function is employed to produce a fresh predicate that functions as the opposite of the IsOddobj predicate. It is utilized to identify even numbers without explicitly verifying odd integers.
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.