binary_function Struct

定义类型能由派生类继承提供一个二进制函数对象的空的基本结构。

template<class Arg1, class Arg2, class Result>
   struct binary_function {
      typedef Arg1 first_argument_type;
      typedef Arg2 second_argument_type;
      typedef Result result_type;
   };

备注

模板结构作为一个基。对于定义窗体的成员函数的选件类:

result_type operator()( constfirst_argument_type&

constsecond_argument_type& ) const

所有此类二进制功能可以引用它们的第一个参数类型为 first_argument_type,其为 second_argument_type的第二个参数类型,因此,它们返回类型为 result_type

示例

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

using namespace std;

template <class Type> class average: 
binary_function<Type, Type, Type> 
{
public:
   result_type operator( ) ( first_argument_type a, 
                 second_argument_type b )
   {
      return (result_type) ( ( a + b ) / 2 );
   }
};

int main( )
{
   vector <double> v1, v2, v3 ( 6 );
   vector <double>::iterator Iter1, Iter2, Iter3;
   
   for ( int i = 1 ; i <= 6 ; i++ )
      v1.push_back( 11.0 / i );

   for ( int j = 0 ; j <= 5 ; j++ )
      v2.push_back( -2.0 * j );

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

   cout << "The vector v2 = ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")" << endl;

   // Finding the element-wise averages of the elements of v1 & v2
   transform ( v1.begin( ),  v1.end( ), v2.begin( ), v3.begin ( ), 
      average<double>( ) );

   cout << "The element-wise averages are: ( " ;
   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")" << endl;
}
  

要求

标头: <functional>

命名空间: std

请参见

参考

binary_function Structure Sample

线程安全性对标准C++库中

标准模板库