过去两天我一直在研究这些功能,并且在使用boost之后我的CPU和Wall时间终于工作了,
最后一根刺我不能让我转过头来,我正在尝试将带参数的函数传递给使用std :: bind返回值的anther函数,
我是学生,这对我来说都是新手,我一边学习,
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
但是从我读过的内容和其他用户看到的代码片段中,我在DoTests()中的代码是正确的。
答案 0 :(得分:2)
类函数有明确的'this'参数,需要作为第一个参数传递:
measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2))
答案 1 :(得分:2)
显然,binarySearch
是类CA1
的成员函数,成员函数始终具有隐式this
参数。当您使用std::bind
成员函数时,您还需要传递this
(请参阅“Using std::bind with member function, use object pointer or not for this argument?”),但这不一定是最佳解决方案......
binarySearch
是否需要成为会员功能? C ++并没有强迫你把所有东西都放在一个类中,你不应该这样做,除非这样做有意义。通常,不需要访问类的私有成员的函数不应该是成员函数(请参阅“How Non-Member Functions Improve Encapsulation”)。此外,标准库已有std::binary_search
。