共用方式為


binary_function Structure Sample

說明如何使用 binary_function 結構中標準樣板程式庫 (STL) 在 Visual C++ 中。

template<class _A1, class _A2, class _R>
   struct binary_function
   {
      typedef _A1 first_argument_type;
      typedef _A2 second_argument_type;
      typedef _R result_type;
   };

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

binary_function< A、 B、 C > 類別當做基底類別中,以允許使用者輕易地定義二元運算子函式接受資料型別 a 和 b 做為引數,且傳回的資料型別 c 的物件。

範例

// binfunc.cpp
// compile with: /EHsc
//
// Structure used: binary_function<A,B,C> - base
//                 class used to create operator
//                 functions taking data types A
//                 and B and returning data type C.

#include <functional>
#include <iostream>

using namespace std ;

class binary_test : public binary_function<binary_test &,int,float>
{
public:
  float value;
  binary_test(){value=10.0;}
  binary_test(float x){value=x;}
  result_type operator<<(second_argument_type arg2);
};

binary_test::result_type
binary_test::operator<<(binary_test::second_argument_type arg2)
{
  value = (float)(((int)value) << arg2);
  cout << "New value after shift is " << value << endl;
  return value;
}

int main(void)
{
  binary_test item;

  cout << "Begin" << endl;
  item = item << 2;
}

Output

Begin
New value after shift is 40

需求

標頭: < 功能 >

請參閱

概念

標準樣板程式庫範例