Share via


binary_negate クラス

指定した二項関数の戻り値を否定するメンバー関数を提供するテンプレート クラス。

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 のコピーを保存します。 これは ###!の_Func (_Left、_Right) を返すようにメンバー関数 operator() を定義します。

binary_negate のコンストラクターは、あまり直接使用されません。 ヘルパー関数 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++ 標準ライブラリ内のスレッド セーフ

標準テンプレート ライブラリ