class template

std::binary_negate

<functional>
template <class Predicate> class binary_negate;
Generate negation of binary function object class
Generates a function object class that negates the behavior of a binary function object class.

binary_negate is generally used as a type. The function not2 (also defined in header <functional>) can be used to directly construct an object of this type.

binary_negate is constructed using a binary function object as argument. A copy of this object is used by its member operator() to return true whenever the original object would return false, and false whenever the object would return true, inverting its behavior.

This class is derived from binary_function and is defined as:

1
2
3
4
5
6
7
8
9
10
11
12
template <class Predicate> class binary_negate
  : public binary_function <typename Predicate::first_argument_type,
                            typename Predicate::second_argument_type, bool>
{
protected:
  Predicate fn;
public:
  explicit binary_negate ( const Predicate& pred ) : fn (pred) {}
  bool operator() (const typename Predicate::first_argument_type& x,
                   const typename Predicate::second_argument_type& y) const
  { return !fn(x,y); }
};


binary_negate class is specifically designed to negate function objects (predicates) derived from binary_function (it requires member first_argument_type and second_argument_type).

Members

constructor
Constructs an object with the opposite behavior than the object passed as its argument.
operator()
Member function returning the opposite of the function object with which the object was constructed.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// binary_negate example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  equal_to<int> equality;
  binary_negate < equal_to<int> > nonequality (equality);
  int foo[] = {10,20,30,40,50};
  int bar[] = {0,15,30,45,60};
  pair<int*,int*> firstmatch,firstmismatch;
  firstmismatch=mismatch (foo,foo+5,bar,equality);
  firstmatch=mismatch (foo,foo+5,bar,nonequality);
  cout << "First mismatch in bar is " << *firstmismatch.second << "\n";
  cout << "First match in bar is " << *firstmatch.second << "\n";
  return 0;
}


Output:

First mismatch in bar is 0
First match in bar is 30

See also