The std::unary_negate() is a wrapper function object returning the complement of the unary predicate it holds. A wrapper function is a subroutine in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation. An object of type unary_negate are generally constructed using function std::not1().
Header File:
#include <functional>
Syntax:
std::unary_negate<class T>
variable_name(Object of class T);
Parameters: The function std::unary_negate() accepts the predicate function object as a parameters and returns the logical compliment of the result generated by calling predicate function.
Return Value: It returns the logical complement of the result by calling predicate function.
Below is the program to illustrate the function std::unary_negate():
Program 1:
// C++ program to illustrate // std::unary_negate to find number // greater than equals 4 in arr[] #include <algorithm> #include <functional> #include <iostream> #include <vector> using namespace std; // Predicate function to find the // count of number greater than or // equals to 4 in array arr[] struct gfg : unary_function<int, bool> { // Function returns true if any // element is less than 4 bool operator()(int i) const { return i < 4; } }; // Driver Code int main() { // Declare vector vector<int> arr; // Insert value from 1-10 in arr for (int i = 1; i <= 10; ++i) { arr.push_back(i); } // Use unary_negate() to find the // count of number greater than 4 unary_negate<gfg> func((gfg())); // Print the count of number using // function count_if() cout << count_if(arr.begin(), arr.end(), func); return 0; } |
7
Explanation: In the above program arrar arr[] has elements from 1 to 10 and the number of elements which is greater than equals to 4 is 7.
Program 2:
// C++ program to illustrate // std::unary_negate to find number // greater than equals 4 in arr[] #include <algorithm> #include <functional> #include <iostream> #include <vector> using namespace std; // Given Structure struct IsOdd_class { // Predicate of this structure bool operator()(const int& x) const { return x % 2 == 1; } typedef int argument_type; } IsOdd_object; // Driver Code int main() { // Use unary_negate function to // to find the compliment of // predicate declare in structure // IsOdd_class unary_negate<IsOdd_class> IsEven_object( IsOdd_object); // Given array int arr[] = { 1, 1, 3, 9, 5 }; int cx; // count with respect to predicate // generated by unary_negate() cx = count_if(arr, arr + 5, IsEven_object); // Print the count cout << "There are " << cx << " elements with even values!" << endl; return 0; } |
There are 0 elements with even values!
Reference: http://www.cplusplus.com/reference/functional/unary_negate/
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- isalpha() and isdigit() functions in C with cstring examples.
- Pointers in C/C++ with Examples
- strtok() and strtok_r() functions in C with examples
- memset() in C with examples
- std::mismatch() with examples in C++
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- set_symmetric_difference in C++ with Examples
- ratio_equal() in C++ with examples
- std::equal_to in C++ with Examples
- quick_exit() function in C++ with Examples
- feclearexcept in C++ with Examples
- Understanding Lvalues, PRvalues and Xvalues in C/C++ with Examples
- multiset lower_bound() in C++ STL with Examples
- multiset upper_bound() in C++ STL with Examples
- multiset max_size() in C++ STL with Examples
- forward_list max_size() in C++ STL with Examples
- std::allocator() in C++ with Examples
- array data() in C++ STL with Examples
- multiset size() in C++ STL with Examples
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.

