std::bind
来源:互联网 发布:mac diva 编辑:程序博客网 时间:2024/05/17 08:01
int f(int, char, double);
auto frev = bind(f, _3, _2, _1); // 翻转参数顺序
int x = frev(1.2, ‘c’, 7); // f(7, ‘c’, 1.2);
此处,auto关键字节约了我们去推断bind返回的结果类型的工作。
bind不仅可以绑定函数,也可以绑定变量
#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;}
注:code from: http://www.cplusplus.com/reference/functional/bind/
text from: http://blog.csdn.net/fjb2080/article/details/7527715
0 0
- std::bind
- std::bind
- std::bind
- std::bind
- std::bind
- std::bind
- std::bind
- std::function std::bind
- std::function std::bind
- std::function 和 std::bind
- lambda, std::function, std::bind
- std::bind std::find 学习
- 笔记-std::function<> && std::bind()
- std::function 和 std::bind
- std::bind与std::function
- std::function std::bind理解
- std::bind std::function 用法
- std::bind 例子
- 用户密码的存储与 Python 示例
- Android中AsyncTask详解.
- 机顶盒的层次结构
- 密码传输问题
- 黑马JAVA知识点总结
- std::bind
- 源码管理十六:标签管理
- HBase shell详情
- LCD1602液晶显示
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- UBUNTU上安装GCC-4.9.2
- 大型网站技术架构读书笔记-伸缩性和扩展性架构
- 新浪微博客户端的开发
- html5支持的触摸键盘类型