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 解决方案,功夫不负有心人,我们 ...
随机推荐
- [kfaka] Apache Kafka:下一代分布式消息系统
简介 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一种快速.可扩展的.设计内在就是分布式的,分区的和可复制的提交 ...
- js 获取图片url的Blob值并预览
1)使用 XMLHttpRequest 对象获取图片url的Blob值 //获取图片的Blob值 function getImageBlob(url, cb) { var xhr = new XMLH ...
- YARN 中的应用程序提交
YARN 中的应用程序提交 本节讨论在应用程序提交到 YARN 集群时,ResourceManager.ApplicationMaster.NodeManagers 和容器如何相互交互.下图显示了一个 ...
- 【ecshop】 完全清除版权信息
完整去掉版权和后门的方法: 去ECSHOP版权,去官方后门,完整版 作者:阿牛 ECShop是一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店.系统开源但不免费,是基于PHP语言及MYSQ ...
- 价格战拉上了Android平板电脑
自从苹果iPad成功以来,平板电脑一直是人们争相追捧的热门产品.其中Android平板电脑已是遍地开花.去年,在移动行业,主流厂商已推出了太多各种款型的Android平板电脑,它们留给2014年的创新 ...
- bootstrap+html5+css3
一.栅格和块阴影 <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 堆叠的水平</title ...
- Android 微信分享,分享到朋友圈与分享到好友,以及微信登陆
extends:http://www.cnblogs.com/android100/p/Android-qq.html 一.申请你的AppID http://open.weixin.qq.com/ 友 ...
- java的synchronized有没有同步的类锁?
转自:http://langgufu.iteye.com/blog/2152608 http://www.cnblogs.com/beiyetengqing/p/6213437.html 没有... ...
- 并查集hdu4424
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 河南省第七届ACM程序设计大赛总结
省赛总结 首先说说比赛时的情况吧,刚开始的时候我的任务就是翻译英文题目,找出比较水的题目,他们两个直接找中文水题切,其实每次比赛我们都是这样配合的,由于他们的判题系统一开始存在问题,交的正确的代码给判 ...




