简体   繁体   English

从另一个函数f0(x,y)定义函数f1(x)对象,但将y设置为固定值c ++

[英]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 =
    [&parameters](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.

相关问题 将函数f(x,y)作为参数固定y - pass function f(x,y) as a parameter with y fixed 如何在C ++中实现函数(f)(x)(y,z)(g)(r)? - How to implement function(f)(x)(y, z)(g)(r) in C++? 使用成员函数在类中设置 POINT2f/VECTOR2f 变量的 x 或 y - Setting x or y of a POINT2f/VECTOR2f variable in a class using a member function C ++ boost或STL`y + = f(x)`类型算法 - C++ boost or STL `y += f(x)` type algorithm “(f(x))+ g(y)”能否确保在C ++中首先调用f(x)? - Does “(f(x))+g(y)” can make sure call f(x) first in C++? c ++:-设置大小为参数X的默认initializer_list参数Y,其中X和Y为函数参数 - c++: - setting default initializer_list argument Y of size argument X, where X and Y are function parameters 你能检查一下标准的 C++ function 是否按下了 F 键(F1、F2 等)吗? - Can you check if an F key (F1, F2, etc) is being pressed with a standard C++ function? C ++:在f的派生类实现中,可以将纯虚函数f的参数x替换为x的子类型吗? - C++: Can the parameter x of a pure virtual function f be replaced with a subtype of x in the derived classes implementation of f? C++ - 使用特定函数计算 x 和 y 的最终值 - C++ - Calculating final values of x and y with a specific function 使用boost :: numeric :: odeint积分非线性函数f&#39;(x,y,z)= a + b * I - Using boost::numeric::odeint to integrate a non-linear function f'(x, y, z) = a + b*I
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM