共用方式為


binary_negate Class

提供否定指定的二進位函式的傳回值的成員函式的樣板類別。

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

參數

  • _Func
    要變成相反值的二進位函式。

  • _Left
    要變成相反值的二進位函式的左運算元。

  • _Right
    要變成相反值的二進位函式的右運算元。

傳回值

二進位函式的負值。

備註

樣板類別儲存二進位 _Func 函式物件的複本。 它會定義成員的函式 operator() 為會傳回 !_Func*(_Left, _Right)。*

直接很少使用 binary_negate 建構函式。 Helper 函式 not2 通常優先於宣告和使用 binary_negator 配置器述詞。

範例

// functional_binary_negate.cpp
// compile with: /EHsc
#define _CRT_RAND_S
#include <stdlib.h>

#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

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

   unsigned int i;
   v1.push_back( 6262 );
   v1.push_back( 6262 );
   unsigned int randVal = 0;
   for ( i = 0 ; i < 5 ; i++ )
   {
      rand_s(&randVal);
      v1.push_back( randVal );
   }

   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<unsigned 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<unsigned int> >(less<unsigned 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<unsigned int>( ) ) );

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

需求

標題: <functional>

std

請參閱

參考

在標準 C++ 程式庫中的執行緒安全

標準樣板程式庫