Skip to content
Related Articles

Related Articles

std::unary_negate() in C++ with Examples
  • Last Updated : 14 Jun, 2020

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :