First-in lambda expression (mostly c++11)

Source: Internet
Author: User

Write a non-cat blog post.

Defined

The lambda expression is defined as follows

[Capture] (parameters) mutable->return-type{statement}

Adult speech is

1.[capture]: Capture list. The capture list always appears at the beginning of the lambda function. In fact, [] is a lambda balloon. The compiler determines whether the next code is a lambda function according to the balloon. The capture list captures variables in the context for use by the lambda function;

2. (parameters): Parameter list. is consistent with the argument list of the normal function. If parameter passing is not required, it can be omitted together with parentheses "()";

The 3.mutable:mutable modifier. By default, a lambda function is always a const function, and mutable can cancel its constant behavior. When the modifier is used, the argument list cannot be omitted (even if the argument is empty);

4.->return-type: return type. Declares the return type of a function in the form of a trace return type. We can also omit the symbol "--" when no return value is required. In addition, if the return type is explicit, you can omit the part and let the compiler deduce the return type;

5.{statement}: Function body. The content is the same as a normal function, but you can use all the captured variables in addition to the parameters you can use.

En, that's about it.

Let's write a few programs to practice practiced hand ~ ~ ~

1. Simple sorting

#include <cstdio> #include <algorithm>int a[10]={0,1,2,3,4,5,6,7,8,9};int main () {Std::sort (a,a+10,[] ( int X,int y) {return x>y;}); for (register int i=0;i<10;i++) printf ("%d\n", A[i]); return 0;}

First-in lambda expression (mostly c++11)

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.