C++11 lambda expression Simple parsing

Source: Internet
Author: User
Tags assert

C++11 has added a lot of features, and lambda expressions are one of them. Assuming you want to understand the c++11 full features,

Suggest go to http://www.open-std.org/see new Standard!


Lambda expressions are available in very many languages, such as Python,java 8

Lambda expressions make it easy to construct anonymous functions, assuming that you have a large number of small functions in your code that are typically called only once. Then it's best to re-form the lambda expression.

The lambda expression specification for c++11 is as follows:

[ c Apture ] ( params ) mutable exception attribute -> ret { body } (1)
[ c Apture ] ( params ) -> ret { body } (2)
[Capture ] ( ) params { Body} (3)
[Capture ] { Body} (4)

Of

    • (1) is a complete lambda expression form.
    • (2) A lambda expression of type Const, which cannot be changed to a value in the Capture ("capture") list.

    • (3) A lambda expression with the return value type omitted. However, the return type of the lambda expression can be performed according to the following rules:
      • Assuming that a return statement is included in a lambda code block, the return type of the lambda expression is determined by the return type of the return statement.
      • Suppose there is no return statement. is similar to the void f (...) function.
    • The list of references is omitted, similar to the No function f ().

The mutable modifier illustrates that code in the body of a lambda expression can alter the variable being captured. and access to the Non-const method of the captured object.

Exception indicates whether the lambda expression throws an exception ( noexcept ). And what exception is thrown, similar to void F()throw(X, Y).

attribute used to declare properties.

In addition,capture specifies a list of external variables that are visible within the code for the lambda expression within the visible domain scope. Detailed explanations such as the following:

    • [a,&b]A variable is captured in the form of a value, and B is captured as a reference.

    • [this]Captures the this pointer in a value manner.

    • [&]Captures all external self-active variables in a reference manner.
    • [=]Captures all external self-active variables in a value manner.
    • []No external, no matter what variables are captured.

In addition, theparams specifies the number of parameters for the lambda expression.

2 detailed c++11 lambda expression model examples:

#include <vector>#include<iostream>#include<algorithm>#include<functional>intMain () {std::vector<int> C {1,2,3,4,5,6,7 }; intx =5; C.erase (Std::remove_if (C.begin (), C.end (), [x] (intN) {returnN <x;}     ), C.end ()); Std::cout<<"C:";  for(auto i:c) {std::cout<< I <<' '; } std::cout<<'\ n'; //the type of a closure cannot is named, but can is inferred with autoAuto func1 = [] (inti) {returni+4; }; Std::cout<<"func1:"<< Func1 (6) <<'\ n'; //Like all callable objects, closures can is captured in Std::function//(this may incur unnecessary overhead)std::function<int(int) > Func2 = [] (inti) {returni+4; }; Std::cout<<"Func2:"<< Func2 (6) <<'\ n'; }//with Boost library # include <boost/function.hpp>typedef boost::function<void (int) > fobject_t;//now function may accept functional objectsvoid process_integers (const fobject_t& f); #include <assert.h> #include & Lt;deque>int Main () {   //lambda function with no parameters that does nothing    proce Ss_integers ([] (int/*i*/) {});   //lambda function that stores a reference    std::d eque <int> ints;    process_integers ([&ints] (int i) {         Ints.push_back (i);   });   //lambda function that modifies its content & nbsp;  std::size_t match_count = 0;    process_integers ([INTs, &match_count] (int i) mutable {& nbsp;       if (ints.front () = = i) {            + + match_count;       }        Ints.pop_front ();   });    assert (match _count = = 6);} void Process_integers (const fobject_t& f) {    static const int data[] = {1, 2, 3, 4, 5, $, 0};     Std::for_each (data, data + 6, f);}


C++11 lambda expression Simple parsing

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.