使用存储在向量中的函数指针

fti*_*sem 1 c++ pointers casting

我有一个包含函数指针的向量:

vector<double (*)(vector<double>)> dY = {d2x,d2y,dx,dy}
Run Code Online (Sandbox Code Playgroud)

在另一个函数中,我有一个for循环迭代这个向量.

for( vector<double>::const_iterator it = dY.begin(); it != dY.end(); ++it){
    vector<double> Y = {0,10,0,10};
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在我想计算d2x(Y),d2y(Y),dx(Y)和dy(Y),但是我没有相关地转换迭代器,所以我可以提供参数.

我怎样才能做到这一点?

非常感谢

sth*_*sth 5

你真的迭代正确的向量吗?it循环中的类型不适合dY矢量.

for( vector<double (*)(vector<double>)>::const_iterator it = dY.begin(); it != dY.end(); ++it)
//          ^^^^^^^^^^^^^^^^^^^^^^^^^ -- You need the correct type here
Run Code Online (Sandbox Code Playgroud)

一旦你有一个正确的迭代器,这应该工作:

double result = (*it)(Y);
Run Code Online (Sandbox Code Playgroud)