参考资料:

http://blog.csdn.net/augusdi/article/details/11771699

  • lambda 表达式的简单语法如下:[capture] (parameters) -> return value { body }

其中[capture]可以选择如下的不同形式:

使用示例:

  • function

std::function对象是对C++中现有的可调用实体的一种类型安全的包裹,std::function 使用

  • bind

std::bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中也颇为有用。 C++0x中,提供了std::bind,它绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制.

注:

对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的占位符。

  • 综合示例:
    1、EmailProcessor.h

    #pragma once
    
    #include <functional>
    #include <string>
    using namespace std; class EmailProcessor
    {
    private:
    function<void (const string&)> _handle_func; public:
    void receiveMessage(const string& str)
    {
    if(_handle_func)
    {
    _handle_func(str);
    }
    } void setHandlerFunc(function<void (const string&)> func)
    {
    _handle_func=func;
    }
    };

    2、MessageStored.h

    #pragma once
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std; class MessageStored
    {
    private:
    vector<string> _store; bool find(const string& str,const string& key)
    {
    return [&](){
    size_t pos=str.find(key);
    return pos!=string::npos && str.substr(pos)==key;
    }();
    } public:
    bool checkMessage(const string& str)
    {
    for(auto iter=_store.begin(),end=_store.end();
    iter!=end;iter++)
    {
    if(find(str,*iter))
    {
    cout<<"Sending Msg "<<str<<endl;
    return true;
    }
    }
    cout<<"no match email"<<endl;
    return false;
    } void addMsgStore(const string str)
    {
    _store.push_back(str);
    }
    };

    3、main.cpp

    #include "EmailProcessor.h"
    #include "MessageStored.h"
    #include <iostream>
    using namespace std; int main()
    {
    MessageStored stored;
    string mails[]={"@163.com","@qq.com","@gmail.com"};
    for(string str:mails)
    {
    stored.addMsgStore(str);
    } EmailProcessor processor;
    auto func=std::bind(&MessageStored::checkMessage,stored,placeholders::_1);
    processor.setHandlerFunc(func);
    processor.receiveMessage("xxx@gmail.com");
    }

注:

&MessageStored::checkMessage是获取类成员函数的地址

C++ lamda、function、bind使用的更多相关文章

  1. Extjs使用Ext.function.bind, 给句柄函数传参

    回调函数updateImage中的key参数,在外部调用时有程序员自己指定. 使用Ext.Function.bind(this.updateImage, this, 'imageUrl', true) ...

  2. C++ 类的成员函数指针 ( function/bind )

    这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...

  3. javascript 中 function bind()

    Function bind() and currying <%-- All JavaScript functions have a method called bind that binds t ...

  4. 为什么React事件处理函数必须使用Function.bind()绑定this?

    最近在React官网学习Handling Events这一章时,有一处不是很明白.代码如下: class Toggle extends React.Component { constructor(pr ...

  5. 【转帖】漫话C++0x(四) —- function, bind和lambda

    实在是觉得此文总是去翻感觉不太好.于是果断转过来了,想看原文的请戳:http://www.wuzesheng.com/?p=2032 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lam ...

  6. ES6下的Function.bind方法

    在JavaScript的使用中,this的指向问题始终是一个难点.不同的调用方式,会使this指向不同的对象.而使用call,apply,bind等方式,可改变this的指向,完成一些令人惊叹的黑魔法 ...

  7. c++11 function bind 测试。

    实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...

  8. C++ TR1 Function Bind

    在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...

  9. 学习C++11的一些思考和心得(1):lambda,function,bind和委托

     1.lambda表达式 lanbda表达式简单地来讲就是一个匿名函数,就是没有名称的函数,如果以前有接触过python或者erlang的人都比较熟悉这个,这个可以很方便地和STL里面的算法配合 st ...

  10. 解决function.bind()方法

    这个 bind 方法只有在 ie10 版本的浏览器才得到原生支持,低于该版本的浏览器下执行时会得到一个 undefined 的错误提示. 于是只好再次上网 google 解决方案,功夫不负有心人,我们 ...

随机推荐

  1. opencascade读取iges并用vtk离散的一些问题

    近期抽时间在弄iges文件内容读取的工作.然后将其离散化在vtk中能够显示处理以及兴许的一些工作.主要目的是识别CAD文件导出的模型,然后进行离散处理.方便兴许的处理.离散工作比較简单.opencas ...

  2. ios 添加动画的方法

    转自文顶顶大神的博客:http://www.cnblogs.com/wendingding/p/3751519.html ios 开发UI中,经常会用添加动画效果的需求,下面就总结一下,添加动画的三种 ...

  3. Python 爬虫知识点 - XPath

    http://cuiqingcai.com/2621.html 一.基础介绍 <bookstore> <book> <title>Harry Potter</ ...

  4. ionic 下拉刷新,上拉加载更多

    1)下拉刷新用的是 ion-refresher,使用示例如下: <ion-refresher pulling-text="Pull to refresh..." on-ref ...

  5. 分享一句话的同时说说遍历map的常用方法

    最近在网上看到一句话,鄙人觉得这是比较经典的一句话,首先要给大家分享哈: 当一个人找不到出路的时候,最好的办法就是将当前能做好的事情做到极致,做到无人能及. Map<String, String ...

  6. kubectl get 输出格式

    常见的输出格式有: * custom-columns=<spec> # 根据自定义列名进行输出,逗号分隔 * custom-columns-file=<filename> # ...

  7. osgearth将视点绑定到一个节点上

    _manip->getSettings()->setTetherMode(osgEarth::Util::EarthManipulator:: TETHER_CENTER ); //设置_ ...

  8. osgEarth2.8添加模型

    #include <osgEarthDrivers/model_simple/SimpleModelOptions> SimpleModelOptions modelOptions; mo ...

  9. Android 手机随音乐振动

    想到做一个Android软件可以在播放音乐的同时手机在音量或者音调高的时候振动一下. 等我熟练Android的时候试试...

  10. Python初学总结

    下边的总结都是在python3上 一.基础 1.输出与输入: 输出:print(变量/字符串) 输入:input() 返回的是字符串 price=input() print(price) 2.pyth ...