std::binary_negate in c++
Standard Template Library (STL) function object adapter std::binary_negate is accessible in C++. It is usually used in conjunction with other algorithms to change the way they behave because it applies a logical negation operation on the output of a binary predicate. A binary predicate is a function or function object that accepts two parameters and yields a boolean result of them. The std::find_if, std::count_if, std::remove_if, and other algorithms that rely on a predicate to define their behavior are frequently used with it.
When you need to reverse the logic of a binary predicate, std::binary_negate comes in assistance. It takes a binary predicate as input when it is called, and you build a new function object that provides the logical negation of the predicate's original result. It can be very useful when you need to count or remove components that do satisfy a given condition or when you need to utilize an algorithm that requires a predicate but you want to look for elements that do not satisfy the supplied condition.
Syntax:
It has the following syntax:
#include<functional> std::binary_negate<PredicateType> negatedPredicate(PredicateType pred);
Explanation of syntax:
- Access the required functionality by including the <functional> header.
- Specify the original binary predicate, which can be an object (such as std::equal_to<int>) or a function.
- Put the original predicate in the constructor's parameter to create an instance of std::binary_negate. The negated predicate is embodied by this example.
- Once the negation operation is applied to the outcome of the original predicate, the negatedPredicate object can be used in algorithms that anticipate a binary predicate.
Methods used in above topic:
1. std::binary_negate<PredicateType> negatedPredicate(PredicateType pred);:
- std::binary_negate is a function object adapter.
- PredicateType is the type of the original binary predicate you want to negate.
- pred is the original binary predicate that you want to negate. It's passed as an argument to the std::binary_negate constructor.
2. Algorithm Functions (e.g., std::count_if):
- Algorithms like std::count_if accept binary predicates as arguments to determine their behavior.
3. Use of std::binary_negate Examples
- The given example uses std::count_if in conjunction with std::binary_negate to count elements according to the logical negation of the supplied predicates.
4. Making a Std::binary_negate Instance:
- As demonstrated in the example, to create a std::binary_negate instance, call its constructor with the original predicate as an argument.
C++ Code:
#include <iostream> // Include for std::cout #include <algorithm> #include <functional> bool is_even(int num) { return num % 2 == 0; } int main() { int numbers[] = {7, 8, 9, 10, 11, 12}; int count_odd = std::count_if(numbers, numbers + 6, std::not1(std::bind2nd(std::ptr_fun(is_even), 0))); // Using of std::binary_negate std::binary_negate<std::equal_to<int>> not_equal(9); int count_not_equal = std::count_if(numbers, numbers + 6, not_equal); std::cout << "Count odd numbers: " << count_odd << std::endl; std::cout << "Count numbers not equal to 9: " << count_not_equal << std::endl; return 0; }
Output:
Count odd numbers: 3 Count numbers not equal to 9: 5
Explanation:
- #include statements: These include necessary headers for input and output (<iostream>) and the Standard Template Library (STL) functionality (<algorithm> and <functional>).
- is_even function: It is a custom function that checks if a number is even. It returns true if the number is even and false otherwise.
- int numbers[]: An array of integers is defined with values {7, 8, 9, 10, 11, 12}.
- count_odd: This variable is used to store the count of odd numbers in the array. It uses the std::count_if algorithm with a custom predicate. The predicate is created using std::not1, std::bind2nd, and std::ptr_fun to apply the is_even function to each element and count the odd numbers.
- not_equal: It is an instance of std::binary_negate created to count numbers not equal to 9 in the array.
- count_not_equal: This variable stores the count of numbers not equal to 9 in the array using std::count_if and the not_equal predicate.
Example 2:
#include <iostream> // std::cout #include <functional> // std::binary_negate, std::equal_to #include <algorithm> #include <utility> int main () { std::equal_to<int> equality; std::binary_negate < std::equal_to<int> > nonequality (equality); int foo[] = {15,25,35,46,70}; int bar[] = {0,15,30,45,60}; std::pair<int*,int*> firstmatch,firstmismatch; firstmismatch = std::mismatch (foo,foo+5,bar,equality); firstmatch = std::mismatch (foo,foo+5,bar,nonequality); std::cout << "First mismatch in bar is " << *firstmismatch.second << "\n"; std::cout << "First match in bar is " << *firstmatch.second << "\n"; return 0; }
Output:
First mismatch in bar is 0 First match in bar is 32764
Conclusion:
The C++ Standard Library, notably the <functional> header, has the binary function adaptor std::binary_negate. It is employed to negate the outcome of an existing binary predicate to produce a new binary predicate. In other terms, it reverses the binary predicate's original Boolean result, returning true in cases where the original predicate would have returned false, and vice versa.