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 解决方案,功夫不负有心人,我们 ...
随机推荐
- ref 属性使用eslint报错
react 使用 ref 报错 ,[eslint] Using string literals in ref attributes is deprecated. (react/no-string-re ...
- GIS-001-gdal软件下载地址
http://www.gisinternals.com/ http://download.gisinternals.com/sdk/downloads/release-1600-x64-gdal-1- ...
- Android UI优化——include、merge 、ViewStub
在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...
- Unity中SendMessage和Delegate效率比较
网上直接搜的代码.需要的使用也简单,所以就不过多说明. 但是网上都说,他们之间的差距,delegate比较快,效果高.怎么个高法呢?还是自己来测试下时间. 故此, 个人之用来比较下时间差别. 一.直接 ...
- js中hover事件时候的BUG以及解决方法
hover事件是我们在开发前段时候遇到的稀松平常的问题,但是有没有发现会出现有一个BUg,比如,你移动到一个元素上,让它执行一个方法,然后你快速的移入移出的时候,他会进行亮瞎你眼睛的频闪效果,而且跟得 ...
- /usr/local/java/jdk1.8.0_11
- MVC AJAX Pager Helper
MVCPager 分页控件: Author:杨涛 http://www.webdiyer.com/mvcpager/demos/ajaxpagers/ https://yunpan.cn/cq4HDc ...
- poj_3261 后缀数组
题目大意 给出一个数字串,找出其中至少重复K次的最长的子串长度. 题目分析 直接用后缀数组来求解,限制height[i]的长度来对排好序的后缀进行分组(这种方法经常在字符串问题中被使用). 先 ...
- pythonday
python学习 http://www.cnblogs.com/fengyuhao/p/7168903.html pythonday http://www.cnblogs.co ...
- 【文智背后的奥秘】系列篇——分布式爬虫之WebKit
版权声明:本文由文智原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/139 来源:腾云阁 https://www.qclou ...




