Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

binary_negate Class

A template class providing a member function that negates the return value of a specified binary function.

template<class Operation>
   class binary_negate
      : public binary_function <
         typename Operation::first_argument_type,
         typename Operation::second_argument_type, 
         bool> 
   {
   public:
   explicit binary_negate(
      const Operation& _Func
   );
   bool operator()(
      const typename Operation::first_argument_type& _Left,
      const typename Operation::second_argument_type& _Right
   ) const;
   };

Parameters

_Func
The binary function to be negated.
_Left
The left operand of the binary function to be negated.
_Right
The right operand of the binary function to be negated.

Return Value

The negation of the binary function.

Remarks

The template class stores a copy of a binary function object _Func. It defines its member function operator() as returning !_Func(_Left, _Right).

The constructor of binary_negate is rarely used directly. The helper function not2 is usually preferred to declare and use the binary_negator adaptor predicate.

Example

// functional_binary_negate.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter1;

   int i;
   v1.push_back( 6262 );
   v1.push_back( 6262 );
   for ( i = 0 ; i < 5 ; i++ )
   {
      v1.push_back( rand( ) );
   }

   cout << "Original vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   // To sort in ascending order,
   // use default binary predicate less<int>( )
   sort( v1.begin( ), v1.end( ) );
   cout << "Sorted vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   // To sort in descending order, 
   // use the binary_negate function
   sort( v1.begin( ), v1.end( ), 
   binary_negate<less<int> >(less<int>( ) ) );

   // The helper function not2 could also have been used 
   // in the above line and is usually preferred for convenience
   // sort( v1.begin( ), v1.end( ), not2(less<int>( ) ) );

   cout << "Resorted vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;
}

Output

Original vector v1 = ( 6262 6262 41 18467 6334 26500 19169 )
Sorted vector v1 = ( 41 6262 6262 6334 18467 19169 26500 )
Resorted vector v1 = ( 26500 19169 18467 6334 6262 6262 41 )

See Also

<functional> Members | Thread Safety in the Standard C++ Library

Show:
© 2015 Microsoft