Lambda expressions provide a concise way to create anonymous function objects. They are defined inline, often within the context where they're used. The syntax generally looks like this:
[capture list](parameter list) -> return type { function body };
Capture list: Specifies variables from the surrounding scope accessible within the lambda. Options include:
[]
: Captures nothing.[=]
: Captures all variables in the surrounding scope by value.[&]
: Captures all variables in the surrounding scope by reference.[=, &var1, &var2]
: Captures all by value except var1
and var2
, which are captured by reference.[var1, &var2]
: Captures var1
by value and var2
by reference.->
, or implicitly deduced by the compiler.Example:
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; std::for_each(numbers.begin(), numbers.end(), [](int x){ std::cout << x * 2 << " "; }); // Output: 2 4 6 8 10 std::cout << std::endl; return 0; }
Functors, or function objects, are classes that overload the function call operator (operator()
). This allows instances of the class to be called like functions.
Example:
#include <iostream> #include <vector> class Doubler { public: void operator()(int x) { std::cout << x * 2 << " "; } }; int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; Doubler doubler; std::for_each(numbers.begin(), numbers.end(), doubler); // Output: 2 4 6 8 10 std::cout << std::endl; return 0; }
The primary difference lies in conciseness and scope. Lambda expressions are significantly more compact for simple operations, eliminating the need to define a separate class. They are also implicitly defined within their scope, making them ideal for one-off operations. Functors, on the other hand, are explicitly defined classes, allowing for more complex logic, member variables to maintain state, and potential reuse across multiple parts of the code. Lambdas are generally limited in their ability to maintain state beyond what's captured from their surrounding scope. Functors can have member variables to store and manipulate state throughout their lifecycle.
Prefer lambda expressions when:
Prefer functors when:
No, you cannot always use lambda expressions and functors interchangeably. While they both represent function objects, their capabilities differ. Lambdas excel in their brevity and inline nature, ideal for short, simple operations. However, they lack the flexibility of full-fledged classes. Functors, being classes, provide more control over state management, member functions, and reusability, but they introduce more boilerplate code. Therefore, the choice depends on the complexity and requirements of the specific task. A complex function with stateful operations is better suited to a functor, while a simple, one-time use operation benefits from a lambda's brevity.
The above is the detailed content of How do I use lambda expressions and function objects (functors) in C ?. For more information, please follow other related articles on the PHP Chinese website!