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
原创粉丝点击