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 are the ways to use the lambda expression of C++?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what are the ways to use the lambda expression of C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the ways to use C++ 's lambda expressions?"

Capture list of 1.lambda expressions

In the previous article, we created a unary predicate that returns true when an integer is divisible by 2. Now if you want to change the specified divisor, you can capture the list through the lambda expression.

Int Divisor=3;// divisor

Auto iElement = find_if (begin, end, [Divisor] (int dividend) {return (dividend% Divisor) = = 0;})

Parameter ([...] Is called a capture list of lambda expressions

General syntax for 2.lambda expressions

Lambda expressions always start with [] and can accept multiple parameters, which can be specified in the capture list ([]) and separated by commas.

[Var1, Var2] (Type& param) {expression;}

Lambda expressions can also accept multiple input parameters, which can be separated by commas:

[S1, S2] (Type1& var1, Type2& var2) {expression;}

If you want to explicitly specify the return type to the compiler, use->, as follows:

[S1, S2] (Type1& var1, Type2& var2)-> ReturnType {Statement1;Statement2; return (value or expressiom);}

3. Lambda expression corresponding to binary function

A binary function takes two arguments and can return a value. The definition is as follows:

[.] (Type1& param1Name, Type2& param2Name) {expression;}

The following demonstrates using the lambda expression as a binary function to multiply the elements in the two containers and store the results in the third container.

# include # include # include using namespace std;int main () {vectorvecMult1,vecMult2;for (int count=0; count

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

Internet Technology

Wechat

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

12
Report