Home > Backend Development > C++ > body text

What is the difference between STL function objects and C++ lambda expressions?

PHPz
Release: 2024-04-25 12:18:01
Original
573 people have browsed it

Function objects and lambda expressions are both tools for creating anonymous functions. The main differences are: Syntax: Function objects use class definitions, while lambda expressions use [] syntax. Scope: Function objects can be used outside the class, whereas lambda expressions are limited to the scope of definition. Capture: Function objects cannot capture external variables, but lambda expressions can capture through capture lists. Overhead: Function object creation overhead is low, lambda expression overhead is high. Reusability: Function objects are usually reusable, lambda expressions are usually single-use.

STL 函数对象与 C++ lambda 表达式的区别是什么?

The difference between STL function objects and C lambda expressions

Function objects and lambda expressions are both used to create in C Tools for anonymous functions. While they have similarities, there are also key differences:

Syntax

  • Function objects: Use classes to declare.
  • lambda expression: Use [] syntax definition.

Scope

  • Function object: Can be used outside the class.
  • lambda expressions: are only valid within the scope in which they are defined.

Capture

  • Function object: Cannot capture external variables.
  • lambda expression: External variables can be captured through the capture list.

Overhead

  • Function object: Low creation overhead.
  • lambda expression: High overhead to create because it requires generating a closure.

Reusability

  • Function objects: Usually reusable.
  • lambda expression: Usually a one-time use.

Practical Case

Suppose we have an array of integers and we want to find the first element that meets a certain condition (for example, the first one is greater than 10 Elements).

Using a function object:

class GreaterThan10 {
public:
    bool operator()(int x) { return x > 10; }
};

int main() {
    int arr[] = {1, 5, 7, 12, 14};
    auto found = find_if(begin(arr), end(arr), GreaterThan10());
    if (found != end(arr)) {
        cout << "First number greater than 10: " << *found << endl;
    }
    return 0;
}
Copy after login

Using a lambda expression:

int main() {
    int arr[] = {1, 5, 7, 12, 14};
    auto found = find_if(begin(arr), end(arr), [](int x) { return x > 10; });
    if (found != end(arr)) {
        cout << "First number greater than 10: " << *found << endl;
    }
    return 0;
}
Copy after login

In this example, the lambda expression is Create an anonymous function that determines whether the integer is greater than 10.

The above is the detailed content of What is the difference between STL function objects and C++ lambda expressions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template