参考资料:

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. MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###

    org.apache.ibatis.exceptions.PersistenceException:  ### Error building SqlSession. ### The error may ...

  2. redis安装之zmalloc.h:55:2: error: #error "Newer version of jemalloc required"错误

    redis是C语言编写的软件,安装前需要编译,需要gcc编译环境,确认安装gcc编译环境后(安装gcc命令:yum install gcc-c++) 在redis解压目录下,确认有Makefile文件 ...

  3. div位置设置

    div居中显示 margin:0 auto div中的内容居中显示 text-algin:center div靠右显示 float:right 设置div元素的右外边距 margin-right:10 ...

  4. Zookeeper(二)-- 客户端操作命令

    一.前提 开启zookeeper服务端,用客户端连接.输入help,查看可使用命令,如下图所示: 操作无非就是增删改查等. 二.增加 格式:create [-s] [-e] path data acl ...

  5. RecyclerView的通用适配器,和滚动时不加载图片的封装

    对于RecyclerView我们需要使用RecyclerAdapter,使用方式与ListViewAdapter类似,具体代码大家可以在网上搜索,这里就只教大家使用封装后的简洁RecyclerAdap ...

  6. C语言编程基础学习字符型数据的ASCII码值为何是负数?

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  7. Bootloader之uBoot简介(转)

    来自http://blog.ednchina.com/hhuwxf/1915416/message.aspx,感谢作者 一.Bootloader的引入从前面的硬件实验可以知道,系统上电之后,需要一段程 ...

  8. cocos2dx游戏--欢欢英雄传说--添加动作

    添加完人物之后接着给人物添加上动作.我们为hero添加4个动作:attack(由3张图片构成),walk(由2张图片构成),hit(由1张图片构成),dead(由1张图片构成):同样,为enemy添加 ...

  9. poj_2709 贪心算法

    poj 2709 painter 题目要求 给定涂料,每套涂料含有3-12种不同的颜色(开始时候给定选用的颜料套的颜色数目),且一套涂料中每种颜色均有50ml.且一套涂料中的任意三种不同的颜色各X m ...

  10. synchronized同步方法

    “非线程安全”其实会在多个线程对同一个对象中的实例变量进行并发访问的时候产生,产生的后果是脏读,也就是取到的数据是被更改过的.而“线程安全”就是以获得的实例变量的值是经过同步处理的,不会出现脏读的现象 ...