zoukankan      html  css  js  c++  java
  • C++ 11 笔记 (四) : std::bind

    std::bind 接受一个可调用的对象,一般就是函数呗。。

    还是先上代码:

    1 void func(int x, int y, int z)
    2 {
    3     std::cout << "hello!" << x << y << z << std::endl;
    4 }

    我们可以通过std::bind调用这个函数:

    1 std::_Bind<true, void, void(*const)(int, int, int), int, int, int> test_bind = std::bind(func, 7, 8, 9);

    其中7,8,9是传给func函数的参数。看这恶心的声明,简直比上一篇博客举得例子还要恶心,所以auto又大显身手了:

    1 auto test_bind = std::bind(func, 7, 8, 9);

    短了好多好多啊。。。

    这时候我们调用:

    1 test_bind();

    就会打印“hello!789”了。

    然后再说一下占位符,我们把test_bind改成这样:

    1 auto test_bind = std::bind(func, std::placeholders::_1, std::placeholders::_2, 9);

    这时候我们可以调用:

    1 test_bind(1, 2);

    就会打印“hello!129”。也就是前两个参数是可以指定的了。(第三个参数不可以)。

    其中 std::placeholders::_1 是占位符,我试了试好像能到 _20 (这。。)

    在我看来,std::bind这个东西用在写事件回调中很好用~~~

  • 相关阅读:
    运算优先级
    (5).plus(3).minus(2);
    画图
    函数的三种角色
    表格排序
    正则表达式
    call apply
    css文本属性和边框属性
    css颜色/字体/背景属性
    css选择器优先级及继承
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3685294.html
Copyright © 2011-2022 走看看