Table of Contents
lambda Syntax
Lambda inside member function
Home Backend Development C++ The meaning of Lambda function in C/C++

The meaning of Lambda function in C/C++

Sep 02, 2023 pm 07:33 PM
significance lambda function c/c

The meaning of Lambda function in C/C++

Lambda Function - A Lambda function is an inline function that does not require any implementation outside the scope of the main program.

Lambda functions can also be used as the value of a variable to be stored. Lambdas can be called objects (called functors) that can be called by functions.

Whenever the compiler encounters the definition of a lambda function, it usually creates a custom lambda object.

The lambda function has more features than a normal function, for example, it has a capture method to capture the variables used. However, captured variables are treated as members of the object.

Sometimes lambda functions are also called "function objects", which have their own scope and can be passed as parameters within ordinary functions. Function. Lambda functions have their own life cycle.

[ ] - Capture

( ) - Parameters (optional)

- Return value (optional)

{...} - Function body.

lambda Syntax

[ ]( int a) -> int { return a-1 ;};
Copy after login

Capture – Capture is a clause through which a lambda function can access variables available in a specific scope or nested block.

We can capture a certain value by using two methods to get the available variables:

  • Capturing an object by name – Capturing an object by name will A lambda function that generates a local copy of the object.

We can understand this concept with the following example -

int main(){
   set s;
   //Adding the elements to set
   int i=20;
   for_each(s.begin(),s.end(), [i](T& elem){
      cout<<elem.getVal()*i<<endl;
   }
}
Copy after login

In the above example, the value is captured by creating a local copy of the lambda function.

  • Capturing objects by reference – Capturing objects by reference allows you to manipulate the context of a lambda function. Therefore, the value captured by a function object or lambda function can change.

We can understand this through the following example-

int main(){
   sets;
   //Adding elements to the set
   int result=0;
   for_each(s.begin(),s.end(), [&result](&T elem){ result+= elem.getVal();});
   cout<<result<<endl;
}
Copy after login

Lambda inside member function

We know that lambda function can be inside any ordinary function used as parameters. For example,

class func{
public:
   func(set<T>s): s1(s){}
   void func(){
      remove_if(s1.begin(),s1.end(), [this](int i) ->bool {return (i<level);});
   }
private:
   set<T>s1;
   int result;
};
Copy after login

The above is the detailed content of The meaning of Lambda function in C/C++. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Thank you for the introduction to the meaning and origin of the bubble stalk. Thank you for the introduction to the meaning and origin of the bubble stalk. Mar 26, 2024 pm 06:36 PM

What’s the meaning of Thank You Bubble? Some friends are very interested in this meme, but don’t know what it means. Below I will introduce the meaning of the thank you bubble meme. Come and take a look. What is the meme of Thank You Bubble? This is actually an emoticon meme. It means that Bubble brings a glass of warm water. It hopes you will be healthy and not get sick. Say &quot;Thank you Bubble&quot; quickly. The meaning of the Thank You Bubble meme is that Bubble has brought something good, I hope you can say &quot;Thank you Bubble&quot;. Thank you Bubble emoticon pack This is Bubble, it brings a glass of warm water, it hopes you will be healthy and not sick, please say &quot;Thank you Bubble&quot;. This is Carl. Carl brought you a glass of water. Carl hopes that you must stay hydrated no matter what. Drink the glass of water and say: Thank you Carl for the water.

In PHP, what does the double question mark (??) operator mean? In PHP, what does the double question mark (??) operator mean? Aug 19, 2023 pm 01:57 PM

PHP7 has added a new operator double question mark (??) operator. In PHP7, the double question mark (??) operator is called the NullCoalescing operator. If the first operand exists and is not NULL, the first operand is returned; otherwise, the second operand is returned. It is evaluated from left to right. The NullCoalescing operator can also be used in chained format. Let us demonstrate the double question mark (??) operator with the following example. Example&lt;?php //$aisnotset echo$a??9??45;?&am

In C/C++, the strcmp() function is used to compare two strings In C/C++, the strcmp() function is used to compare two strings Sep 10, 2023 am 11:41 AM

Thefunctionstrcmp()isabuilt-inlibraryfunctionanditisdeclaredin“string.h”headerfile.Thisfunctionisusedtocomparethestringarguments.Itcomparesstringslexicographicallywhichmeansitcomparesboththestringscharacterbycharacter.Itstartscomp

What other upgrades have there been to Cancun? What's the point of the Cancun upgrade? What other upgrades have there been to Cancun? What's the point of the Cancun upgrade? Apr 14, 2024 am 09:37 AM

Ethereum has continued to develop since its birth. The previously completed Cancun upgrade is a phased upgrade of Ethereum aimed at improving the scalability, security and sustainability of the Ethereum network. Although the Cancun upgrade is an important milestone for Ethereum 2.0, it does not mean that the upgrade work has been completed. The subsequent Ethereum 2.0 is still in the stage of development and improvement. Therefore, what upgrades will the currency circle expect after the upgrade of Cancun? Attracting much attention, official statements stated that after the Cancun upgrade is officially implemented in 2024, the Goerli test network will no longer be used, and there are no specific plans for subsequent upgrades. Next, the editor will tell you in detail. What other upgrades have there been to Cancun? There are no specific plans for other upgrades after the Cancun upgrade. In addition, according to the latest news, Ethereum Cancun upgrade 20

In C/C++, the fseek() function is used to move the position of the file pointer in the file. In C/C++, the fseek() function is used to move the position of the file pointer in the file. Sep 02, 2023 pm 03:57 PM

fseek() is used in C language to move the file pointer to a specific location. Offsets and streams are the targets of pointers, and they are given in function arguments. If successful, it returns zero. If unsuccessful, it returns a non-zero value. The following is the syntax of fseek() in C language: intfseek(FILE*stream,longintoffset,intwhence) Here are the parameters used in fseek(): stream− This is the pointer used to identify the stream. offset − This is the number of bytes from the position. whence−This is where the offset is added. whence is given by the following constants

What is the lambda function in Python and why do we need it? What is the lambda function in Python and why do we need it? Aug 25, 2023 pm 02:41 PM

In this article, we will learn about the lambda function in Python and why we need it, and see some practical examples of lambda functions. What is lambda function in Python? A Lambda function is often called an "anonymous function" and is the same as a normal Python function, except that it can be defined without a name. The >def keyword is used to define ordinary functions, while the lambda keyword is used to define anonymous functions. However, they are limited to single-line expressions. They, like regular functions, can accept multiple arguments. Syntax lambdaarguments:expression This function accepts any number of inputs, but only evaluates and returns an expression. Lamb

The role and significance of the Linux Home directory The role and significance of the Linux Home directory Feb 22, 2024 am 11:06 AM

The Linux operating system is an open source operating system known for its stability, security and flexibility. In Linux systems, the Home directory is the default working directory for each user after logging in, and is also the place where the user's personal files and settings are stored. The role and significance of the Home directory is very important. This article will explore the role and significance of the Linux Home directory. First, the Home directory provides each user with a private space for storing personal files and settings. Each user has a unique

Python beginners must learn: Master the basic usage of lambda functions Python beginners must learn: Master the basic usage of lambda functions Feb 02, 2024 pm 06:41 PM

Essential for beginners: To master the basic usage of lambda functions in Python, specific code examples are required. Overview: Python is a simple and easy-to-learn programming language. It has attracted the love of many programmers with its concise and flexible syntax. In Python, a lambda function is a special anonymous function that can be defined directly where the function is required without giving it a name. This article will introduce the basic use of lambda functions and provide specific code examples to help beginners better understand

See all articles