Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A detailed introduction of the Lambda function in Clipper 11

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "the detailed introduction of the Lambda function in Category 11". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of the Lambda function in Clippers 11.

Caterpillar 11 finally knows how to add anonymous functions to the language. Anonymous functions can facilitate coding in many cases, which will be mentioned below. Anonymous functions in many languages, such as clocked expressions, are implemented in Lambda expressions. Lambda expressions are also known as lambda functions. I'll call it the Lambda function later on.

To understand the usefulness of the Lambda function, be sure to understand the automatic type inference in C++: http://blog.csdn.net/kaitiren/article/details/22302767

Basic Lambda function We can define a Lambda function like this: [cpp] view

Plaincopy

# include

Using namespace std

Int main ()

{

Auto func = [] () {cout int {return 1;}

So generally speaking, the form of the lambda function is: [cpp] view

Plaincopy

[captures] (params)-> ret {Statments;}

The use of the Lambda function assumes that you have designed an address book class. Now you need to provide a function to query the address book, possibly by name, by address, or by a combination of both. If you write a function for all these situations, you must be on your knees. So you should provide an interface that allows users to easily customize their own query methods. You can use the lambda function to do this here. [cpp] view

Plaincopy

# include

# include

Class AddressBook

{

Public:

/ / using a template allows us to ignore the differences between functors, function pointers

/ / and lambda

Template

Std::vector findMatchingAddresses (Func func)

{

Std::vector results

For (auto itr = _ addresses.begin (), end = _ addresses.end (); itr! = end; + + itr)

{

/ / call the function passed into findMatchingAddresses and see if it matches

If (func (* itr))

{

Results.push_back (* itr)

}

}

Return results

}

Private:

Std::vector _ addresses

}

As you can see from the above code, the argument provided by the findMatchingAddressses function is of type Func, which is a generic type. A function should be passed in during use, and then the function should be executed separately for each entry in the address book. If the return value is true, then the entry meets the filtering requirements of the user, and it should be included in the result. So how do you pass in parameters of this Func type? [cpp] view

Plaincopy

AddressBook global_address_book

Vector findAddressesFromOrgs ()

{

Return global_address_book.findMatchingAddresses (

/ / we're declaring a lambda here; the [] signals the start

[] (const string& addr) {return addr.find (".org")! = string::npos;}

);

}

As you can see, we define a lambda function directly when we call the function. The parameter type is

[cpp] view

Plaincopy

Const string& addr

The return value is of type bool. If users want to query in different ways, they only need to define different lambda functions.

Variable interception in the Lambda function in the above example, the lambda function uses the parameters of the function body and its internal information, not external information. We imagine a scenario where we read a name from the keyboard and then define an anonymous function using the lambda function to find out if there is anyone with the same name in the address book. Then the lambda function is bound to be able to use variables in the external block, so we have to use the variable interception function (Variable Capture). [cpp] view

Plaincopy

/ / read in the name from a user, which we want to search

String name

Cin > > name

Return global_address_book.findMatchingAddresses (

/ / notice that the lambda function uses the the variable 'name'

[&] (const string& addr) {return name.find (addr)! = string::npos;}

);

As you can see from the above code, our lambda function can already use the variable name in the external scope. One of the biggest differences in this lambda function is that the & symbol is added in the middle of []. This tells the compiler to intercept variables. This allows the body of the lambda function to use external variables. If no symbols are added, the compiler will not intercept variables.

Here are the options for intercepting various variables:

[] do not intercept any variables

[&} intercept all variables in the external scope and use them as references in the function body

[=] intercept all variables in the external scope and make a copy for use in the function body

[=, & foo] intercepts all variables in the external scope and makes a copy for use in the function body, but uses a reference to the foo variable

[bar] intercept bar variables and make a copy for weight use in the function without intercepting other variables

[this] intercepts the this pointer in the current class. This option is added by default if & or = is already used.

Lambda function and STL

The introduction of lambda function provides great convenience for the use of STL. For example, when you want to facilitate a vector, you have to write: [cpp] view

Plaincopy

Vector v

V.push_back (1)

V.push_back (2)

/ /...

For (auto itr = v.begin (), end = v.end (); itr! = end; itr++)

{

Cout ret {body} (2) [capture] (params) {body} (3) [capture] {body} (4)

Among them

(1) is a complete lambda expression form

(2) an lambda expression of type const, which cannot change the value in the capture ("capture") list.

(3) the lambda expression of the return type is omitted, but the return type of the lambda expression can be inferred according to the following rules:

If the lambda code block contains a return statement, the return type of the lambda expression is determined by the return type of the return statement.

If there is no return statement, it is similar to void f (...). Function.

The parameter list is omitted, similar to the no-parameter function f ().

The mutable modifier indicates that the code inside the lambda expression can modify the captured variable and can access the non-const method of the captured object.

Exception indicates whether the lambda expression throws an exception (noexcept) and what kind of exception it throws, similar to void f () throw (X).

y).

Attribute is used to declare properties.

In addition, capture specifies a list of external variables that are visible within the code of the lambda expression in the visible domain, as explained below:

A variable is captured as a value and b is captured as a reference.

[this] captures the this pointer as a value.

[&] capture all external automatic variables by reference.

[=] capture all external automatic variables as values.

[] does not capture any external variables.

In addition, params specifies the parameters of the lambda expression.

A specific example of a clocked 11 lambda expression:

# include # include int main () {std::vector c {1pje 2pc 3pje 4je 5pas 6pas 7}; int x = 5; c.erase (std::remove_if (c.begin (), c.end (), [x] (int n) {return n < x;}), c.end ()); std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report