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

How to use the Lambda function in C++

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the Lambda function in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Lambda function in C++".

One function syntax

When we usually call a function, it is the function name that needs to be called, but the anonymous function does not need the function name, and it is written directly where it needs to be called. For those who have not used it before, it may be confusing to see this syntax at first glance.

The basic syntax format of Category 11 is as follows:

[capture] (parameters)-> return_type {/ *... * /}

(1) [capture]: [] is the transfer mode of external variables, values, references, etc., as follows

[] / / indicates the invocation of external parameters in the domain before the definition of lambda; [=] / indicates that the external parameters pass the value directly [&] / / indicates that the external parameters pass references and can modify the value. When the default catcher is &, subsequent simple catchers must not start with &. When the default catcher is =, the subsequent simple catcher must start with &. [X, & y] / / x is captured by value, y is captured by reference [&, x] / / x is explicitly captured by value. Other variables will be captured by reference [=, & z] / / z is explicitly captured by reference Other variables will be captured by value

(2) (parameters): () is the formal parameter, which is the same as the formal parameter of the ordinary function.

(3)-> return_type:- > is followed by the return type of the lambda function, such as-> int,-> string, etc. In general, the compiler deduces the return value of the lambda function, so this part can be omitted.

(4) {/ *... * /}: {} is the body of the function, just like the ordinary function.

Application of two functions 1. Use in ordinary functions

First of all, the definition, execute the following sentence, will not run the function!

Std::function add = [] (int aline int b)-> int {return a + b;}

The above code, you know that the lambda function return type for the int type, but the left side of the function can not be directly assigned to the int variable (the compiler will report an error), because here is the definition of the lambda function, so the left for the function pointer type variable, generally lazy to write function pointer type, directly assigned to the auto type variable, as follows.

Auto add = [] (int aline int b)-> int {return a + b;}

The following is the use and operation of the function!

How do I run the lambda function and get the return value of the function? To execute the function, you need to see the following code:

Auto add = [] (int apenint b)-> int {return a + b;}; int result = add (1mem2)

Or it may be as follows:

Auto add = [] (int apenint b)-> int {return a + b;}; int (* func_ptr) (int,int) = add; int result = func_ptr (1Jing 2); 2. Used in qt signal slots

I think the labmda function is very suitable for use in signal slots.

(1) the returned function pointer can be directly used in the connect function.

(2) the slot function may be relatively simple, and only this place can be used, which can save the slot function declaration and make the code look simpler and the business code more centralized.

Basic use

Connect (sys, & SYSClass::sig_1, this, [=] (int index) {. });

The lambda function is defined when the signal slot is bound, and the slot function, namely the lambda function, is executed only when the signal is received.

Use demonstration

Perform animation after mTime time, and delete object after animation execution, which is very suitable for animation operation when the pop-up window closes. (widgetPtr is the pointer of the pop-up window)

QTimer::singleShot (mTime,widgetPtr, [=] () {QPropertyAnimation * pAnimation = new QPropertyAnimation (widgetPtr, "windowOpacity", widgetPtr); pAnimation- > setDuration (1000); pAnimation- > setEasingCurve (QEasingCurve::InCirc); pAnimation- > setStartValue (1000); pAnimation- > setEndValue (0.0); pAnimation- > start (); connect (pAnimation,&QPropertyAnimation::finished, [=] {delete widgetPtr;}) ); 3. Use in std::sort sorting function

In the use of the sorting function of the standard library, you can use the function callback to customize the sorting comparison rules.

(1) the sort function provides sorting algorithms; (2) floatList is a container variable that provides data structures and data; and (3) an iterator is used to connect the two of them.

And our sort function can use the third parameter (function callback) as the basis for sorting, the parameter can be written as the lambda function, so the comparison method is written here in the sorting function, which can make the code more straightforward, simple and centralized. The reference code is as follows:

Std::sort (floatList, floatList + N, [] (float a, float b) {return a < b;})

The above lambda function is called every time the iterator in the sort function runs to make a sort comparison.

Note:

(1) the parameter of lambda function is (float, float), and the type must be the same as the element type of the container.

(2) at run time, the iterator automatically passes the parameters into the callback function for you.

(3) the return value of the callback function is fixed as bool, which can be automatically determined by the compiler without writing here.

Thank you for your reading, the above is the content of "how to use the Lambda function in C++". After the study of this article, I believe you have a deeper understanding of how to use the Lambda function in C++, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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