← Back to C++ Course | Chapter 5: Functions | Lesson 11 of 13

C++ के Lambda Functions

एक lambda एक छोटा nameless function है जो आप वहीं लिखते हैं जहाँ आपको ज़रूरत हो, एक quick sticky-note instruction की तरह। यह अपने आस-पास से values भी याद रख सकता है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. lambda के अंदर एक variable इसे capture किए बिना इस्तेमाल करना, जो एक compile error है।
  2. [x] से value से capture करना और lambda से original बदलने की उम्मीद करना, जब यह सिर्फ अपनी copy modify करता है (और इसे mutable चाहिए)।
  3. एक local को [&] से reference से capture करना और variable के destroy होने के बाद lambda call करना, जो एक dangling reference है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.