1. std::function是可调用对象的包装器;std::bind是将可点用对象和其参数一起进行绑定,且绑定后的结果可以使用std::function对象进行保存,并延迟调用到需要调用的时候;
  2. 在C++中,可调用实体主要包括函数,函数指针,函数引用,可以隐式转换为函数指定的对象,或者实现了opetator()的对象(即C++98中的functor)。C++0x中,新增加了一个std::function对象,std::function对象是对C++中现有的可调用实体的一种类型安全的包裹(我们知道像函数指针这类可调用实体,是类型不安全的)。
  3. bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中也颇为有用。C++98中,有两个函数bind1st和bind2nd,它们分别可以用来绑定functor的第一个和第二个参数,它们都是只可以绑定一个参数。各种限制,使得bind1st和bind2nd的可用性大大降低。C++0x中,提供了std::bind,它绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制,由用户指定,这个bind才是真正意义上的绑定,有了它,bind1st和bind2nd就没啥用武之地了,因此C++0x中不推荐使用bind1st和bind2nd了,都是deprecated了。
  4. C++11 的 lambda 表达式规范如下:

    [ capture ] ( params ) mutable exception attribute -> ret { body } (1)  
    [ capture ] ( params ) -> ret { body } (2)  
    [ capture ] ( params ) { body } (3)  
    [ capture ] { body } (4)  

    其中

    • (1) 是完整的 lambda 表达式形式,
    • (2) const 类型的 lambda 表达式,该类型的表达式不能改捕获("capture")列表中的值。
    • (3)省略了返回值类型的 lambda 表达式,但是该 lambda 表达式的返回类型可以按照下列规则推演出来:
      • 如果 lambda 代码块中包含了 return 语句,则该 lambda 表达式的返回类型由 return 语句的返回类型确定。
      • 如果没有 return 语句,则类似 void f(...) 函数。
    • 省略了参数列表,类似于无参函数 f()。

    mutable 修饰符说明 lambda 表达式体内的代码可以修改被捕获的变量,并且可以访问被捕获对象的 non-const 方法。

    exception 说明 lambda 表达式是否抛出异常(noexcept),以及抛出何种异常,类似于void f() throw(X, Y)。

    attribute 用来声明属性。

    另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表,具体解释如下:

    • [a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获。
    • [this] 以值的方式捕获 this 指针。
    • [&] 以引用的方式捕获所有的外部自动变量。
    • [=] 以值的方式捕获所有的外部自动变量。
    • [] 不捕获外部的任何变量。

    此外,params 指定 lambda 表达式的参数。

  5. 测试用例:

    1、std::function作为回调函数使用示例

    

class A
{
std::function<void()> callback_;
public:
A(const std::function<void()>& f) :callback_(f){}
void Notify()
{
callback_();
}
};
class Foo
{
public:
void operator()(void)
{
std::cout << __FUNCTION__ << std::endl;//get the func name }
};
void Test8()
{
Foo foo;
A a(foo);
a.Notify();
}

  2、std::function 和std::bind 结合使用:

void call_when_event(int x, const std::function<void(int)>& func)
{
if (!(x & ))
{
//cout << x << endl;
func(x);
}
} void call_when_event1(int x,int y, const std::function<void(int,int)>& func)
{
if (!(x & ))
{
//cout << x << endl;
func(x,y);
}
}
void output(int x)
{
cout << x << endl;
}
void output_add_2(int x,int y)
{
cout << x + y << endl;
}
void Test7()
{
auto fr = std::bind(output, std::placeholders::_1);
for (int i = ; i < ; i++)
{
call_when_event(i, fr);
}
std::cout << std::endl; auto fr1 = std::bind(output_add_2, std::placeholders::_1,std::placeholders::_2);
for (int i = ; i < ; i++)
{
call_when_event1(i, i,fr1);
}
}
3、lambda 测试用例:
返回vector中大于5小于10的元素的个数:
void Test5()
{
std::vector<int> coll;
for (int i = ; i <= ; i++)
{
coll.push_back(i);
}
int count = std::count_if(coll.begin(), coll.end(), [](int x){return x > && x < ; });
std::cout << count << std::endl; }

 

C++ 中std::function 、std::bind的使用和lambda的使用的更多相关文章

  1. c++11——std::function和bind绑定器

    c++11中增加了std::function和std::bind,可更加方便的使用标准库,同时也可方便的进行延时求值. 可调用对象 c++中的可调用对象存在以下几类: (1)函数指针 (2)具有ope ...

  2. C++11 学习笔记 std::function和bind绑定器

    C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...

  3. C++中的仿函数,std::function和bind()的用法

    1.仿函数:又叫std::function,是C++中的一个模板类 2.C语言中的函数指针: int  add(int a,int b) { return a+b; } typedef int (*f ...

  4. std::function,std::bind复习

    #include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...

  5. C++ 11 std::function std::bind使用

    cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码: auto closeItem = MenuItemImage::create( "CloseNorm ...

  6. C++11学习笔记之三lamda表达式,std::function, std::bind

    //lamda //first lamda [] {}; // second lamda []() //or no need () when paramater is null { std::cout ...

  7. C++中str1::function和bind

    在C++的TR1中(TechnologyReport)中包括一个function模板类和bind模板函数,使用它们能够实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类的非静态成员函数 ...

  8. C++11 中的function和bind、lambda用法

    std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo ...

  9. 理解javascript中的Function.prototype.bind

    在初学Javascript时,我们也许不需要担心函数绑定的问题,但是当我们需要在另一个函数中保持上下文对象this时,就会遇到相应的问题了,我见过很多人处理这种问题都是先将this赋值给一个变量(比如 ...

  10. 浅析 JavaScript 中的 Function.prototype.bind() 方法

    Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个对象,bind() 方法会创建一个函数,函数体内this对象的值会被绑定到传入bind() 函数 ...

随机推荐

  1. 【062有新题】OCP 12c 062出现大量之前没有的新考题-16

    choose one Which users are created and can be used for database and host management of your DBaaS da ...

  2. [Vuejs] webpack+vue-cli打包如何引用相对路径

    默认情况下通过webpack+vuec-li打包的css.js等资源,路径都是绝对的,即static在根目录下,假如部署到带有文件夹目录的项目中,资源路径就会出错,如何解决. 1.修改资源引用相对路径 ...

  3. 桶排序和计数排序的理解实现和比较(Java)

    比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比较排序.在排序的最终结果里,元素之间的次序依赖于它们之间的比较.每个数都必须和其他数进行比较,才能确定自己的位置.比较排序的优势是 ...

  4. POJ 2601

    #include<iostream> #include<iomanip> #include<stdio.h> using namespace std; int ma ...

  5. Python相关在线文档手册地址

    Python相关: 五星推荐:http://python.usyiyi.cn/ Python 2.7官方中文文档:http://doc.iplaypy.com/python2/  英文:    htt ...

  6. 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)—— 0.学习目标

    Understand the key computations underlying deep learning, use them to build and train deep neural ne ...

  7. StreamSets学习系列之StreamSets的集群安装(图文详解)

    不多说,直接上干货! 若是集群安装 需要在对应节点执行相同的操作. 见 StreamSets学习系列之StreamSets支持多种安装方式[Core Tarball.Cloudera Parcel . ...

  8. Unity生成WebService代理类

    普通的.net程序中,如果我们想引用webService,只需在项目中右键选择添加服务引用,然后在地址栏中输入Webservice地址,单击“转到”按钮,找到服务后再单击确定,Visual Studi ...

  9. Java NIO系列教程(九) ServerSocketChannel

    Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...

  10. php安装扩展模块后,重启不生效的原因及解决办法

    在lnmp运维环境中,我们经常会碰到有些php依赖的扩展模块没有安装,这就需要后续添加这些扩展模块.在扩展被安装配置后,往往会发现php-fpm服务重启后,这些扩展并没有真正加载进去!下面就以一个示例 ...