std::bind小记

std::bind函数(是一个函数模版),用来绑定函数的某些参数并生成一个新的std::function对象。

如何来确定绑定的是函数的第几个参数,引入std::placeholders命名空间:

_1,函数调用的第一个参数,

_2第2个参数,

_N第N个参数

目录

目录

1.绑定普通函数

没有占位符

1个占位符

2个占位符

返回类型转换

2.绑定类里面的成员

静态函数

成员函数

成员变量

3.VS. bind1st bind2nd

简化和增强了 bind1st 和 bind2nd

组合方便



1.绑定普通函数

double my_divide (double x, double y) {return x/y;}

使用上述的普通函数,分成一下几种情况,对原函数进行基本的转化,使用了bind可以使得不用新写函数了

没有占位符

两个输入参数都传入固定的值(10 和 2),不需要占位符placeholder

 std::function<double(void)> fn_five = std::bind (my_divide,10,2);               // returns 10/2
 std::cout << fn_five() << '\n';                          // 5

1个占位符

占位原函数中的第一个输入参数。第二个参数传入固定的值(2)。

std::function<double(double)> fn_half = std::bind (my_divide,_1,2);               // 新函数内容变成 returns x/2
std::cout << fn_half(10) << '\n';                        // 5 

2个占位符

颠倒了第一个和第二个输入参数的位置

std::function<double(double,double)> 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

 

2.绑定类里面的成员

struct MyPair {
  double a,b;
  double multiply() {return a*b;}
  static void saySomething(){std::cout << "I want to sleep" << std::endl}
};

静态函数

不需要传入实例

std::function<void()> bind_static_member_func = std::bind(&MyPair::saySomething);

bind_static_member_func();

成员函数

需要传入一个实例,非静态成员函数的第一个参数是this指针,实例本身

  MyPair ten_two {10,2};
  std::function<double(MyPair)> bound_member_fn = std::bind (&MyPair::multiply,_1); // returns x.multiply()
  std::cout << bound_member_fn(ten_two) << '\n';           // 20

成员变量

成员变量的第一参数

&MyPair::a 的类型应该是 std::function<double(MyPair)>

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

3.VS. bind1st bind2nd

简化和增强了 bind1st 和 bind2nd

bind可以将多元(n>1)函数转成一元函数或者(n-1)元函数,而bind1st 和 bind2nd只是将二元算子转换成一个一元算子。

//查找元素值大于10的元素的个数
int count = count_if(coll.begin(), coll.end(), std::bind1st(less<int>(), 10));
// 等价于
int count = count_if(coll.begin(), coll.end(), bind(less<int>(), 10, _1));

 

//查找元素之小于10的元素 
int count = count_if(coll.begin(), coll.end(), std::bind2nd(less<int>(), 10));
//等价于
int count = count_if(coll.begin(), coll.end(), bind(less<int>(), _1, 10));

 

组合方便

auto f = bind(std::logical_and<bool>(), bind(std::greater<int>(),_1,5), bind(std::less_equal<int>(),_1,10));
int count = count_if(dest.begin(), dest.end(), f);

 

参考

http://www.cplusplus.com/reference/functional/bind/

https://www.cnblogs.com/qicosmos/p/3302144.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值