C++ के Lambda Functions
In this page:
auto lambda_name = [](type param) {
return expression;
};
lambda_name(argument);
Basic Lambda Syntax
एक lambda एक छोटा, unnamed function है जिसे आप वहीं define कर सकते हैं जहाँ आपको ज़रूरत हो, एक expression के अंदर, [](parameters) { body } syntax इस्तेमाल करते हुए।
ये उन छोटे logic pieces के लिए खासकर useful हैं जिनकी आपको सिर्फ एक बार ज़रूरत हो, जैसे std::sort में सीधे pass किया गया एक custom comparison।
उदाहरण: Basic Lambda Syntax
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Define a lambda (anonymous function) stored in sayHello
auto sayHello = []() { std::cout << "Hello from a lambda!" << std::endl; };
sayHello();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameters वाला Lambda
Lambdas exactly regular functions की तरह ही parameters accept करते हैं, parentheses के अंदर लिखे गए — [](int a, int b) { return a + b; } दो ints लेता है और उनका sum return करता है, exactly जैसे same signature वाला एक named function करता।
उदाहरण: Lambda with Parameters
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Define a lambda (anonymous function) stored in add
auto add = [](int a, int b) { return a + b; };
// Print to the console with cout
std::cout << add(3, 4) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Lambda Return Types
एक lambda का return type आमतौर पर इसकी body से automatically infer होता है, लेकिन आप इसे एक trailing arrow से explicitly specify कर सकते हैं, जैसे [](int x) -> double { return x / 2.0; }, जो कभी-कभी ज़रूरी होता है जब compiler intended type खुद unambiguously infer न कर सके।
उदाहरण: Lambda Return Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Define a lambda (anonymous function) stored in divide
auto divide = [](int a, int b) -> double { return (double)a / b; };
// Print to the console with cout
std::cout << divide(7, 2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Lambda Capture Clause
Capture clause [] control करता है कि lambda surrounding scope से कौन से variables अपनी body के अंदर access कर सकता है: [=] सब कुछ value से (एक copy) capture करता है, जबकि [&] सब कुछ reference से (originals तक direct access) capture करता है, और आप specific variable names भी list कर सकते हैं सिर्फ उन्हें capture करने के लिए।
उदाहरण: Lambda Capture Clause
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare factor and set it to 10
int factor = 10;
// Define a lambda (anonymous function) stored in multiply
auto multiply = [=](int x) { return x * factor; };
// Print to the console with cout
std::cout << multiply(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Generic Lambdas
C++14 से, एक lambda के parameters को एक specific type की बजाय auto से mark करना — [](auto x, auto y) { return x + y; } — एक generic lambda बनाता है जो अलग call sites पर अलग argument types accept कर सकता है, spirit में एक template function जैसा लेकिन inline लिखा गया।
उदाहरण: Generic Lambdas
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Define a lambda (anonymous function) stored in add
auto add = [](auto x, auto y) { return x + y; };
// Print to the console with cout
std::cout << add(2, 3) << std::endl;
// Print to the console with cout
std::cout << add(2.5, 3.5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- lambda के अंदर एक variable इसे capture किए बिना इस्तेमाल करना, जो एक compile error है।
[x]से value से capture करना और lambda से original बदलने की उम्मीद करना, जब यह सिर्फ अपनी copy modify करता है (और इसेmutableचाहिए)।- एक local को
[&]से reference से capture करना और variable के destroy होने के बाद lambda call करना, जो एक dangling reference है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions