[英]Define a function f1(x) object from an another function f0(x,y), but setting y to a fixed value c++
Suppose I have a function 假设我有一个功能
void f0(double x, double parameters[]) { ... }
and I want to define a function object 我想定义一个功能对象
std::function <void (double x) >f1
such that, for example, f1(x) = f0(x,a)
where a
is a specified set of parameters (eg double parameters[4] = {1.0, 2.9, 6.2, 2.1}
) 这样,例如,
f1(x) = f0(x,a)
其中a
是一组指定的参数(例如double parameters[4] = {1.0, 2.9, 6.2, 2.1}
)
How would I do this? 我该怎么做? My thoughts are to try to have a function that inputs
a
outputs f1, but I'm not sure how to do this. 我的想法是尝试有其输入功能
a
输出F1,但我不知道如何做到这一点。
The motivation of this is that, in essence, I have yet another function FUNC in a library, that inputs a function with a single double input, but I want more flexibility to add parameters to that function 这样做的动机是,从本质上讲,我在库中还有另一个函数FUNC,该函数使用一个double输入输入一个函数,但是我希望有更大的灵活性来向该函数添加参数
This is very simple with a lambda: 使用lambda非常简单:
std::function<void(double)> f1 =
[¶meters](double x) { f0(x, parameters); };
Alternatively, you could use std::bind
. 另外,您可以使用
std::bind
。
using namespace std::placeholders;
std::function<void(double)> f2 = std::bind(f0, _1, parameters);
But I much prefer lambdas for almost any situation. 但我几乎在任何情况下都更喜欢lambda。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.