Resources:
http://blog.csdn.net/augusdi/article/details/11771699
- The simple syntax for a lambda expression is as follows: [Capture] (parameters), return value {body}
Where [capture] can choose from the following different forms:
Examples of Use:
The Std::function object is a type-safe package for existing callable entities in C + +, std::function using
Std::bind is a mechanism that can pre-bind certain parameters of a specified callable entity to an existing variable, producing a new callable entity that is useful in the use of a callback function. In c++0x, the std::bind is provided, and the number of parameters it binds is unrestricted, and the specific parameters of the binding are unrestricted.
Note:
For non-binding parameters, you need to pass std::p laceholders in, start from _1, and then increment. Placeholder is a placeholder for pass-by-reference.
- Comprehensive Example:
1, EmailProcessor.h
#pragmaOnce#include<functional>#include<string>using namespacestd;classemailprocessor{Private: Function<void(Const string&) >_handle_func; Public: voidReceiveMessage (Const string&str) { if(_handle_func) {_handle_func (str); } } voidSethandlerfunc (function<void(Const string&) >func) {_handle_func=func; }};
2, MessageStored.h
#pragmaOnce#include<string>#include<vector>#include<iostream>using namespacestd;classmessagestored{Private: Vector<string>_store; BOOLFindConst string& STR,Const string&key) { return[&] () {size_t pos=Str.find (key); returnpos!=string:: NPOs && str.substr (pos) = =key; }(); } Public: BOOLCheckmessage (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; } voidAddmsgstore (Const stringstr) {_store.push_back (str); }};
3, Main.cpp
#include"EmailProcessor.h"#include"MessageStored.h"#include<iostream>using namespacestd;intMain () {messagestored stored; stringmails[]={"@163.com","@qq. com","@gmail. com"}; for(stringstr:mails) {Stored.addmsgstore (str); } Emailprocessor processor; Auto Func=std::bind (&messagestored::checkmessage,stored,placeholders::_1); Processor.sethandlerfunc (func); Processor.receivemessage ("[email protected]");}
Note:
&messagestored::checkmessage is the address that gets the class member function
C + + LAMDA, function, bind use