cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : binary_negate
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_funct...
· pointer_to_unary_function
· unary_negate

-

binary_negate class template
template <class Predicate> class binary_negate;
<functional>

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:

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

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

not2 Return negation of binary function object (function template)
unary_negate Generate negation of unary function object class (class template)
binary_function Binary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us