C + + LAMDA, function, bind use

Source: Internet
Author: User

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:

    • function

The Std::function object is a type-safe package for existing callable entities in C + +, std::function using

    • Bind

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.