你什么时候在 C++ 中使用函数对象?

2024-08-14C/C++开发问题
8

免费vpn 本文介绍了你什么时候在 C++ 中使用函数对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn vpn下载 free vpn

问题描述

我看到函数对象经常与 STL 算法一起使用.函数对象是不是因为这些算法而产生的?你什么时候在 C++ 中使用函数对象?它有什么好处?

I see function 免费vpn下载 objects used often free vpn together with vpn下载 STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits?

推荐答案

正如 jdv 所说,使用函子代替函数指针,函数指针更难优化和内联编译器;此外,函子的一个基本优点是它们可以轻松地在调用它们之间保持状态1,因此它们可以根据其他时间被调用而不同地工作,以某种方式跟踪它们的参数用过,...

As said jdv, functors are used vpn下载 instead of function vpn下载 pointers, that are harder to optimize and inline for the compiler; moreover, a fundamental advantage of functors is that they can 免费vpn easily vpn下载 preserve a state between calls to them1, so they vpn下载 can work differently depending on 免费vpn the other times they have been called, keep track somehow of the parameters they have used, ...

例如,如果您想对两个整数容器中的所有元素求和,您可以执行以下操作:

For example, if you vpn free want to sum all the elements in two free vpn containers of ints you may do something like this:

struct
{
  vpn free   int sum;
    void operator()(int element) { sum+=element; 免费vpn }
} functor;
functor.sum=0;
functor = std::for_each(your_first_container.begin(), your_first_container.end(), functor);
functor = std::for_each(your_second_container.begin(), your_second_container.end(), 免费vpn functor);
std::cout<<"The sum of all the 免费vpn下载 elements is: "<<functor.sum<<std::endl;

<小时>

  1. 实际上,正如 R Samuel Klatchko 在下面指出的那样,它们可以支持多个独立状态,每个函子实例一个:
  1. Actually, as vpn free R Samuel Klatchko pointed out below, they can support multiple independent states, one for each functor instance:
稍微更精确的说法是函子可以支持多个独立状态(函数可以通过既不是线程安全的也不是可重入的静态/全局变量).
A slightly more precise statement is that 免费vpn下载 functors can support multiple independent states (functions free vpn can support a single state via statics/globals 免费vpn which is neither 免费vpn下载 thread-safe nor reentrant).

函子使您能够使用更复杂的状态,例如共享状态(静态字段)和私有状态(实例字段).然而,很少使用这种进一步的灵活性.

Functors enables you to use vpn free even more complicated states, for example 免费vpn下载 a shared state (static fields) and a private state (instance fields). However vpn下载 this further flexibility is rarely used.

这篇关于你什么时候在 C++ 中使用函数对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function 免费vpn argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 免费vpn std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in vpn下载 STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6