C++ lamda、function、bind使用
参考资料:
- 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使用的更多相关文章
- Extjs使用Ext.function.bind, 给句柄函数传参
回调函数updateImage中的key参数,在外部调用时有程序员自己指定. 使用Ext.Function.bind(this.updateImage, this, 'imageUrl', true) ...
- C++ 类的成员函数指针 ( function/bind )
这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...
- javascript 中 function bind()
Function bind() and currying <%-- All JavaScript functions have a method called bind that binds t ...
- 为什么React事件处理函数必须使用Function.bind()绑定this?
最近在React官网学习Handling Events这一章时,有一处不是很明白.代码如下: class Toggle extends React.Component { constructor(pr ...
- 【转帖】漫话C++0x(四) —- function, bind和lambda
实在是觉得此文总是去翻感觉不太好.于是果断转过来了,想看原文的请戳:http://www.wuzesheng.com/?p=2032 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lam ...
- ES6下的Function.bind方法
在JavaScript的使用中,this的指向问题始终是一个难点.不同的调用方式,会使this指向不同的对象.而使用call,apply,bind等方式,可改变this的指向,完成一些令人惊叹的黑魔法 ...
- c++11 function bind 测试。
实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...
- C++ TR1 Function Bind
在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...
- 学习C++11的一些思考和心得(1):lambda,function,bind和委托
1.lambda表达式 lanbda表达式简单地来讲就是一个匿名函数,就是没有名称的函数,如果以前有接触过python或者erlang的人都比较熟悉这个,这个可以很方便地和STL里面的算法配合 st ...
- 解决function.bind()方法
这个 bind 方法只有在 ie10 版本的浏览器才得到原生支持,低于该版本的浏览器下执行时会得到一个 undefined 的错误提示. 于是只好再次上网 google 解决方案,功夫不负有心人,我们 ...
随机推荐
- 提高Web性能的前端优化技巧总结
- redis配置密码认证,通过密码可以进行连接
需求说明: 今天配置了一台redis服务器,想要也和其他的数据库一样配置用户名/密码的方式进行登录. 查找了一下,没看到配置用户名的地方,就是有认证密码,所以就做了测试,在此进行记录. 操作过程: 1 ...
- ADO.net方法
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...
- android开发源代码分析--多个activity调用多个jni库的方法
android开发源代码分析--多个activity调用多个jni库的方法 有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知 ...
- sqlite数据库下载安装和初步操作和所遇到的问题near "sqlite3":syntax error
1.下载sqlite数据库:http://www.sqlite.org/download.html 假设是在window上安装须要在 Windows 区下载预编译的二进制文件.如图下载下载 sqlit ...
- try catch finally的执行顺序
1.将预见可能引发异常的代码包含在try语句块中. 2.如果发生了异常,则转入catch的执行.catch有几种写法: catch 这将捕获任何发生的异常. catch(Exception e) 这将 ...
- .Net WebAPI文档自动化
1.在VS2015中右键选择工程: 2.点击NuGet程序包: 3.输入Swagger在线搜索: 4.安装:Swagger.Net.UI和Swashbuckle包: 5.打开SwaggerNet.cs ...
- 在联网时,两台linux服务器传输文件方法
登陆服务器root用户命令:su - root 传输文件命令:scp +需要传输linux系统文件+空格+目标linux服务器的用户名@服务器ip地址:+传输的文件路径:例:scp /mnt/work ...
- C++新式转型
本文对四种标准C++的类型转换符:static_cast.dynamic_cast.reinterpret_cast.和const_cast进行了介绍,通过本文应当能够理解这四个类型转换操作符的含义. ...
- 在create-react-app的脚手架里面使用scss
之前用vue-cli脚手架的时候,只需要引进sass需要的依赖包便可以引入scss,但是在create-react-app的时候,发现除了需要引入sass依赖,还需要进行配置: 不管用什么方法进行sa ...




