我目前正在尝试实现一个函数,该函数将为该函数输入任何其他函数和一组有效的输入值,并返回该函数的结果以及打印执行它需要多长时间。创建可变模板函数来测量和执行其他功能
这里是我到现在为止:
template<typename T, typename... Tail>
T measureAndExecute(const function<T(Tail...)> f, Tail... tail) {
high_resolution_clock::time_point time1 = high_resolution_clock::now();
T res = f(tail...);
high_resolution_clock::time_point time2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(time2 - time1).count();
cout << duration << " milliseconds" << endl;
return res;
}
我尝试的东西像这样运行它:
int res = measureAndExecute(function<int(vector<int>&, vector<bool>&, unsigned long)> fibonacci, terms, calculated, n-1);
这是一个功能来查找斐波纳契数列的术语。
当我尝试运行它,我得到以下错误:
error: expected '(' for function-style cast or type construction
有人可以请给我前进的道路上或如何进行的想法?
看看你传递什么作为第一个参数:'函数& ,矢量&,unsigned long)> fibonacci' - 这看起来不正确。如果你想从'fibonacci'构造一个'std :: function',你不应该把'fibonacci'放在圆括号中,例如'function &,vector &,unsigned long)>(fibonacci) ? –
TerraPass
我想你可能会发现std :: bind有趣。 – ZivS
@TerraPass我会试一试。 – soltzu