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

What is the use of Lambda expression in C++

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

Share

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

This article mainly shows you "what is the use of Lambda expressions in C++", the content is simple and clear, and I hope it can help you solve your doubts, so let me lead you to study and learn about "what is the use of Lambda expressions in C++".

Overview

Lambda expressions in C++ 11 are used to define and create anonymous function objects to simplify programming. The syntax of Lambda is as follows:

[capture list] (parameter) mutable or exception declaration-> return value type {function body} / / calculate the sum of two values auto func = [] (int a, int b)-> int {return astatb;}; / / when the return value type is determined, you can ignore the return value auto func = [] (int a, int b) {return astatb;}; / / call int sum = func (1, 3); parsing captures the list

The Lambda expression is equivalent to a class, so the capture list is the class member passed to that class. For example:

Class Labmda {public: const int test; Labmda (int value): test (value) {} public: int run (int a, int b) {return a + b + test;}} int main () {int test = 10; auto func = Labmda (test); int sum = func.run (1,3);} / int main () {int test = 10 using Lambda expression Auto func = [test] (int a, int b) {return a + b + test;}; int sum = func (1,3);}

The capture list has the following format:

The format describes the local variable before the Lambda expression without any parameter [=], including the this of the class in which it belongs. The variable passes the local variable before the [&] Lambda expression by value, including the this of the class in which it belongs. The variable passes the value of the local variable a before the Thisa Lambda expression of the class in which the [this] Lambda expression is located by reference, or you can pass in multiple values. Such as the reference keyword declaration of the local variable a before the [a, b] [& a] Lambda expression

Keyword declarations are rarely used and are not recommended and can be ignored.

Mutable

Valid when the capture list is passed as a value, and with this keyword added, you can modify the Lambda class member with the const modifier. For example:

Int test = 10 test / compile error, test members cannot modify auto func = [test] (int a, int b) {test = 8; return a + b + test;}; / / compile normally auto func = [test] (int a, int b) mutable {test = 8; return a + b + test;}

It is important to note here that the modification of the Lambda class member test does not change the value of the external int test.

Exception

The exception declaration is used to specify the exception thrown by the function. For example, you can use throw (int) to throw an exception of integer type.

The sample capture list is passed by value int test = 10 int auto func = [=] (int a, int b) {return a + b + test;}; auto func2 = [test] (int a, int b) {return a + b + test;}; int sum = func (1,3); / / sum equals 14

It is important to note that the value of test in the func expression is only updated before the expression:

Int test = 10 auto func = [=] (int a, int b) {auto a + b + test;}; test = 5 sum sum = func (1,3); / / sum is still equal to 14 capture lists passing int test = 10 auto func = [&] (int a, int b) {test = 5; return a + b + test;}; auto func2 = [& test] (int a, int b) {test = 5; return a + b + test;}; int sum = func (1,3) / / sum equals nine, and test equals five

Here the value of test in the func expression can be updated at any time:

Int test = 10 auto func = [&] (int a, int b) {return a + b + test;}; test = 5 sum sum = func (1, 3); / / sum equals 9; test equals 5 is all the content of the article "what is the use of Lambda expressions in C++?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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