【发布时间】:2018-05-26 03:28:20
【问题描述】:
我正在从用于求解 ODE 的类层次结构转移到用于求解 ODE 系统的类。
在我使用单个函数的实现中,我使用以下内容来存储我的函数:
std::function<const Type(const Type, const Type)> numericalFunction
我有一个包装器来评估数值函数:
Type f (Type t, Type u) const noexcept { return numericalFunction(t,u); }
现在我要解决方程组,所以我需要存储多个函数。我尝试使用std::vector 存储函数集,如下所示:
std::vector<std::function<const Type(const Type,const Type)>> numericalFunction;
我希望能够使用与上述问题相同的语法。也就是说,
f[0](12,32); 应该执行numericalFunction.at(0)(12,32);
dfdt[0](t, u); 应该执行(numericalFunction.at(0)(t, u+eps) - numericalFunction.at(0)(t, u))/eps;
如何编写代码来允许这样的语法?
编辑我有问题..现在我需要更改功能
std::vector<std::function<const Type(const Type,const Type)>> numericalFunction;
变成了:
std::vector<std::function<const Type(const Type,const std::vector<Type>)>> numericalFunction;
派生类不起作用
【问题讨论】:
-
return numericalFunction.at(0)(t,u);没有意义,numericalFunction是std::function。 -
Type f (size_t function_id, Type t, Type u) const noexcept { numericalFunction.at(function)(t,u); }这样可以吗?正如@YSC 所说,at不是std::function的成员函数。 -
说实话,我不知道你在问什么
-
auto numFun1 = [](double t , double u) {return -20*u+20*sin(t)+cos(t) ; } ;不应该是auto numFun1 = [](double t , double u)->double {return -20*u+20*sin(t)+cos(t) ; } ;? -
你想如何选择一个函数?该函数是否被赋予了唯一的 ID?如果是,是否从 0 开始,是否连续?
标签: c++ class vector operator-overloading