【发布时间】:2014-10-23 19:25:49
【问题描述】:
过去两天我一直在研究这些功能,在使用 boost 后,我的 CPU 和 Wall 时间终于可以工作了,
最后一个我无法解决的问题,我正在尝试将一个带参数的函数传递给另一个函数,该函数使用 std::bind 返回一个值,
我是一名学生,这对我来说是全新的,边走边学,
int CA1::binarySearch(vector<int> v, int target)
{
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom) / 2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
double CA1::measure(std::function<void()> function) {
auto startCpu = boost::chrono::process_real_cpu_clock::now();
auto startWall = boost::chrono::process_system_cpu_clock::now();
function();
auto durationCpu = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_real_cpu_clock::now() - startCpu);
auto durationWall = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_system_cpu_clock::now() - startWall);
double cpuTime = static_cast<double>(durationCpu.count()) * 0.000001;
double wallTime = static_cast<double>(durationWall.count()) * 0.000001;
/*return static_cast<double>(duration.count()) * 0.000001;*/
cout << "Cpu time " << cpuTime << endl;
cout << "Wall time " << wallTime << endl;
return cpuTime;
}
void CA1::DoTests() {
auto time = measure(std::bind(binarySearch, vectorUnordered, 2));
}
我得到的错误是:
error C3867: 'CA1::binarySearch': function call missing argument list; use '&CA1::binarySearch' to create a pointer to member
但从我阅读的内容和其他用户看到的代码 sn-ps 来看,我在 DoTests() 中的代码是正确的。
【问题讨论】:
-
因为
binarySearch是一个非静态成员函数,你需要像&CA1::binarySearch这样限定名称,并绑定CA1的实例,该方法将被调用,例如std::bind(&CA1::binarySearch, this, vectorUnordered, 2)