Lambda Expressions in OOP
Understanding anonymous functions in C++
What are Lambda Expressions?
Lambda expressions are anonymous function objects that can be defined inline. They provide a convenient way to create function objects at the location where they are needed, making the code more concise and readable.
Lambda Expressions क्या हैं? (हिंदी में)
Lambda expressions anonymous function objects हैं जो inline define किए जा सकते हैं। यह function objects को create करने का एक convenient way provide करते हैं जहां वह needed होते हैं, जिससे code more concise और readable हो जाता है।
Key Points:
- Anonymous Functions: No need for separate function definitions
- Capture Clauses: Access variables from enclosing scope
- Type Inference: Automatic type deduction
- Inline Definition: Define where needed
Types of Lambda Expressions
Basic Lambda
Simple function without captures
Capture Lambda
Accesses variables from enclosing scope
Generic Lambda
Uses auto parameters
Lambda Expression Rules
Rule | Description |
---|---|
Capture List | Specify variables to capture |
Parameter List | Define function parameters |
Return Type | Optional, can be deduced |
Body | Function implementation |
Simple Example
#include <iostream> #include <vector> #include <algorithm> #include <string> // Function demonstrating lambda expressions void lambdaExample() { std::cout << "\nLambda Expressions Example:" << std::endl; // Basic lambda auto add = [](int a, int b) { return a + b; }; std::cout << "Basic lambda: " << add(5, 3) << std::endl; // Lambda with capture int multiplier = 10; auto multiply = [multiplier](int x) { return x * multiplier; }; std::cout << "Capture lambda: " << multiply(5) << std::endl; // Lambda with reference capture std::string prefix = "Value: "; auto printWithPrefix = [&prefix](int value) { std::cout << prefix << value << std::endl; }; printWithPrefix(42); // Lambda in algorithm std::vector<int> numbers = {1, 2, 3, 4, 5}; std::cout << "\nVector before transform: "; for (int n : numbers) std::cout << n << " "; // Transform using lambda std::transform(numbers.begin(), numbers.end(), numbers.begin(), [](int n) { return n * n; }); std::cout << "\nVector after transform: "; for (int n : numbers) std::cout << n << " "; // Generic lambda auto print = [](const auto& x) { std::cout << x << std::endl; }; print(42); print("Hello"); print(3.14); // Lambda with mutable int counter = 0; auto increment = [counter]() mutable { return ++counter; }; std::cout << "\nMutable lambda: " << increment() << std::endl; std::cout << "Mutable lambda: " << increment() << std::endl; } int main() { lambdaExample(); return 0; }
उदाहरण की व्याख्या (Example Explanation)
इस उदाहरण में lambda expressions के different aspects को demonstrate किया गया है:
- Basic Lambda: Simple function without captures
- Capture Lambda: Variables को enclosing scope से access करना
- Reference Capture: Variables को reference से capture करना
- Generic Lambda: Auto parameters का use करना
- Mutable Lambda: State को modify करना
Benefits of Lambda Expressions
Lambda Expressions के फायदे
-
Code Organization
- Functions को जहां needed हैं वहीं define कर सकते हैं
- Code को more readable बनाता है
-
Flexibility
- Variables को capture कर सकते हैं
- Different types के parameters accept कर सकते हैं
-
Performance
- Inline optimization possible है
- Function call overhead कम होता है
Best Practices
- Use capture by reference only when necessary
- Keep lambda expressions short and focused
- Use auto for lambda return type when possible
- Consider using named lambdas for complex logic
- Be careful with mutable lambdas