Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-模板函数接受绑定函数作为参数并将其传递给另一个函数_C++_Templates_C++11 - Fatal编程技术网

C++ C++;-模板函数接受绑定函数作为参数并将其传递给另一个函数

C++ C++;-模板函数接受绑定函数作为参数并将其传递给另一个函数,c++,templates,c++11,C++,Templates,C++11,在回答前面的问题()时,我发现在将函数作为另一个函数的参数传递时,可以使用std::bind方法绑定参数。然后如何将其传递给另一个函数?中间功能(repeatFunction)和计时器的代码如下所示: template<typename F> double timer(F function) { clock_t tstart, tend; tstart = clock(); function(); tend = clock(); return ((d

在回答前面的问题()时,我发现在将函数作为另一个函数的参数传递时,可以使用std::bind方法绑定参数。然后如何将其传递给另一个函数?中间功能(repeatFunction)和计时器的代码如下所示:

template<typename F>
double timer(F function) {
   clock_t tstart, tend;

   tstart = clock();
   function();
   tend = clock();

   return ((double)tend - tstart) / CLOCKS_PER_SEC;
}

template<typename F>
std::vector<double> repeatFunction(F function) {
   std::vector<clock_t> numCalls(9);
   std::vector<double> info;

   std::generate(numCalls.begin(), numCalls.end(), [&](){ timer(function); });
   info.push_back(mean(numCalls));
   info.push_back(standardDeviation(numCalls));
   return info;
}
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cmath>
#include <ctime>
#include <iostream>
#include "functions.h"

int main(void) {
   std::vector<double> x;
   x.push_back(53.0);
   x.push_back(61.0);
   x.push_back(49.0);
   x.push_back(67.0);
   x.push_back(55.0);
   x.push_back(63.0);

   std::vector<double> info = repeatFunction(std::bind(mean<double>, x));

   return 0;
}
模板
双定时器(F功能){
时钟开始,开始;
tstart=时钟();
函数();
倾向=时钟();
每秒返回((双)tend-tstart)/时钟;
}
模板
std::向量重复函数(F函数){
std::载体细胞(9);
std::矢量信息;
std::generate(numCalls.begin(),numCalls.end(),[&](){timer(function);});
信息推回(平均数);
信息推回(标准偏差(numCalls));
退货信息;
}
代码接受一个传递的函数并运行多次,然后返回函数运行所用的时间。函数(F函数)从main绑定,如下所示:

template<typename F>
double timer(F function) {
   clock_t tstart, tend;

   tstart = clock();
   function();
   tend = clock();

   return ((double)tend - tstart) / CLOCKS_PER_SEC;
}

template<typename F>
std::vector<double> repeatFunction(F function) {
   std::vector<clock_t> numCalls(9);
   std::vector<double> info;

   std::generate(numCalls.begin(), numCalls.end(), [&](){ timer(function); });
   info.push_back(mean(numCalls));
   info.push_back(standardDeviation(numCalls));
   return info;
}
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cmath>
#include <ctime>
#include <iostream>
#include "functions.h"

int main(void) {
   std::vector<double> x;
   x.push_back(53.0);
   x.push_back(61.0);
   x.push_back(49.0);
   x.push_back(67.0);
   x.push_back(55.0);
   x.push_back(63.0);

   std::vector<double> info = repeatFunction(std::bind(mean<double>, x));

   return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“functions.h”
内部主(空){
std::向量x;
x、 推回(53.0);
x、 推回(61.0);
x、 推回(49.0);
x、 推回(67.0);
x、 推回(55.0);
x、 推回(63.0);
std::vector info=repeatFunction(std::bind(mean,x));
返回0;
}
我是否需要以某种方式获取参数,然后在repeatFunction中重新绑定它们


编辑:这实际上似乎是std::generate的问题,而不是传递的函数调用的问题。任何关于如何使generate与传递的函数调用一起工作的提示都将不胜感激。

std::generate接收的谓词需要返回一个值:

[&] { return timer(function); }
//    ^^^^^^

您得到的错误是什么?在/usr/include/c++/4.7/algorithm:63:0中包含的文件中,从main.cpp:2:/usr/include/c++/4.7/bits/stl_algo.h:void std::generate(_FIter,_FIter,_Generator)[with _FIter=u gnu cxx::u normal iterator;_Generator=repeat function(F)[with F=std:_Bind]:]“:functions.h:50:4:required from'std::vector repeatFunction(F)[with F=std::_Bind]'main.cpp:29:72:required from here/usr/include/c++/4.7/bits/stl_algo.h:5083:2:error:void value没有被忽略,因为它应该是这样的哇,我想我已经盯着这个方向太久了。再次感谢你的帮助。