STL bind1st bind2nd bind 的使用

STL bind1st bind2nd bind 的使用

  • bind1st什么时候用?

bind1st()和bind2nd()把二元函数转化为一元函数,方法是绑定其中一个参数。 这两种方法在 C++11 里已经 deprecated 了,建议使用新标准的 bind()。

  • bind1st原型?
template <class Operation, class T>
  binder1st<Operation> bind1st (const Operation& op, const T& x)
{
  return binder1st<Operation>(op, typename Operation::first_argument_type(x));
}
template <class Operation> class binder1st
  : public unary_function <typename Operation::second_argument_type,
                           typename Operation::result_type>
{
protected:
  Operation op;
  typename Operation::first_argument_type value;
public:
  binder1st ( const Operation& x,
              const typename Operation::first_argument_type& y) : op (x), value(y) {}
  typename Operation::result_type
    operator() (const typename Operation::second_argument_type& x) const
    { return op(value,x); }
};
  • 工作原理?

bind1st有两个参数,一个是二元操作类,一个是被这个二元操作类调用的第一个参数。binder1st是一个一元操作类,初始化构造函数把二元操作类和参数保存下来,当调用实际运算事,调用二元操作类

  • bind1st例子:
// bind1st example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () 
{
  int numbers[] = {10,20,30,40,50,10};
  int cx;
  cx = count_if (numbers, numbers+6, bind1st(equal_to<int>(),10) );
  cout << "There are " << cx << " elements that are equal to 10.\n";
  return 0;
}
  • binder1st例子:
// binder1st example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  binder1st < equal_to<int> > equal_to_10 (equal_to<int>(),10);
  int numbers[] = {10,20,30,40,50,10};
  int cx;
  cx = count_if (numbers,numbers+6,equal_to_10);
  cout << "There are " << cx << " elements equal to 10.\n";
  return 0;
}
  • bind例子:

bind不仅仅可以转换二元运算符,可以占位多元函数,对第几个参数进行占位。

// bind example
#include <iostream>     // std::cout
#include <functional>   // std::bind

// a function: (also works with function object: std::divides<double> my_divide;)
double my_divide (double x, double y) {return x/y;}

struct MyPair {
  double a,b;
  double multiply() {return a*b;}
};

int main () {
  using namespace std::placeholders;    // adds visibility of _1, _2, _3,...

  // binding functions:
  auto fn_five = std::bind (my_divide,10,2);               // returns 10/2
  std::cout << fn_five() << '\n';                          // 5

  auto fn_half = std::bind (my_divide,_1,2);               // returns x/2
  std::cout << fn_half(10) << '\n';                        // 5

  auto fn_invert = std::bind (my_divide,_2,_1);            // returns y/x
  std::cout << fn_invert(10,2) << '\n';                    // 0.2

  auto fn_rounding = std::bind<int> (my_divide,_1,_2);     // returns int(x/y)
  std::cout << fn_rounding(10,3) << '\n';                  // 3

  MyPair ten_two {10,2};

  // binding members:
  auto bound_member_fn = std::bind (&MyPair::multiply,_1); // returns x.multiply()
  std::cout << bound_member_fn(ten_two) << '\n';           // 20

  auto bound_member_data = std::bind (&MyPair::a,ten_two); // returns ten_two.a
  std::cout << bound_member_data() << '\n';                // 10

  return 0;
}
上一篇:简单工厂模式和工厂方法模式


下一篇:微信小程序开发(3)事件绑定