The use of lambda in the boost library

Source: Internet
Author: User



Many programming languages such as Lisp, Python, and C # already have lambda before C + + introduces lambda to the boost library. The initial motivation for the boost library to create Lambda is to solve the problem of having a large number of small function object definitions when using standard libraries. In fact, we can actually declare and define when we need to use these function objects, that is, in-place. The Lambda library greatly simplifies the syntax of the standard library by creating an anonymous lambda expression that replaces the real-name function object.
Let's look at a Hello world-class lambda expression:
(cout << _1 << "<< _3 <<" << _2) ("Welcome", "Cnblog", "to");
where _1, _2, and _3 are called placeholders to represent the first few parameters.
In the article "Use of the bind for boost library," We use bind to find the number of elements that are greater than 5 but less than 10 in an integer container, and now we're trying to do this with lambda.
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), _1>5 && _1<10);
As you can see, the use of lambda makes the syntax more concise and logical and clearer, relative to the binding of the function object using Bind.
Another very important concept in lambda expressions is the delay of constants and variables, for example:
For_each (Vcttemp.begin (), Vcttemp.end (), cout << "<< _1);
The intent of the function is to add a space before each element of the output, but since cout << ' is not a lambda expression, it will be output evaluated immediately. To achieve the purpose of deferred output, we need to use the constant function to create a lambda functor.
For_each (Vcttemp.begin (), Vcttemp.end (), cout << constant (') << _1);
The above is an example of a constant delay call, and the following is a deferred invocation of a variable.
int nIndex = 0;
For_each (Vcttemp.begin (), Vcttemp.end (), cout << ++nindex << _1<<endl);
Similarly, since cout << ++nindex is not a lambda expression, it will only be evaluated once. In order to output the ordinal at each invocation, you need to use the Var function to create a lambda functor.
For_each (Vcttemp.begin (), Vcttemp.end (), cout << ++var (nIndex) << _1<<endl);
Alternatively, you can assign a lambda expression to a function to implement deferred invocation.
function<void (int, int) > FADD = cout << _1 << "+" << _2 << "=" <<_1+_2 << Endl;
(FADD);
FADD (3,4);
The above describes some of the basic functions of the Lambda library. The more powerful of the Lambda Library is that it provides some function templates for controlling the structure, such as: If_then, If_then_else, If_then_else_return, While_loop, Do_while_loop, For_ Loops, switch_statement, and so on. The following is an example of If_then_else, which describes its usage.
For_each (Vcttemp.begin (), Vcttemp.end (), if_then_else (_1%2==0,cout<<_1<< ' \ n ', Cout<<constant (" Odd ") << ' \ n '));



Reprint http://www.cnblogs.com/hujian/archive/2009/06/22/1508075.html

If you have copyright issues, please contact qq:858668791

The use of lambda in the boost library

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.