C++ 使用std::bind将函数传递到函数中

C++ 使用std::bind将函数传递到函数中,c++,boost,bind,std,C++,Boost,Bind,Std,在过去的两天里,我一直在研究这些功能,在使用boost之后,我的CPU和墙壁时间终于开始工作了 最后一个我无法理解的问题是,我试图将一个带参数的函数传递给另一个函数,该函数使用std::bind返回一个值 我是一名学生,这对我来说是全新的,边学习边学习 int CA1::binarySearch(vector<int> v, int target) { int top, bottom, middle; top = vecSize - 1; bottom =

在过去的两天里,我一直在研究这些功能,在使用boost之后,我的CPU和墙壁时间终于开始工作了

最后一个我无法理解的问题是,我试图将一个带参数的函数传递给另一个函数,该函数使用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));
}

但从我阅读的内容和其他用户看到的代码片段来看,我在DoTests()中的代码是正确的。

类函数有显式的“this”参数,需要作为第一个参数传入:

measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2))

类函数具有显式的“this”参数,该参数需要作为第一个参数传入:

measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2))

显然,
binarySearch
是类
CA1
的成员函数,并且成员函数总是有一个隐式
参数。当您将
std::bind
与成员函数一起使用时,您还需要传递
(请参见“”),但这不一定是最佳解决方案


binarySearch
是否需要成为成员函数?C++不会强迫你把所有东西都放在课堂里,你不应该这样做,除非这样做是有意义的。通常,不需要访问类的私有成员的函数不应该是成员函数(请参见“”)。此外,标准库已经有了。

显然,
binarySearch
是类
CA1
的成员函数,并且成员函数总是有一个隐式
参数。当您将
std::bind
与成员函数一起使用时,您还需要传递
(请参见“”),但这不一定是最佳解决方案


binarySearch
是否需要成为成员函数?C++不会强迫你把所有东西都放在课堂里,你不应该这样做,除非这样做是有意义的。通常,不需要访问类的私有成员的函数不应该是成员函数(请参见“”)。此外,标准库已经有了。

因为
binarySearch
是一个非静态成员函数,您需要限定名称,如
&CA1::binarySearch
,以及绑定将调用该方法的
CA1
实例,例如
std::bind(&CA1::binarySearch,this,vectorundered,2)
由于
binarySearch
是一个非静态成员函数,您需要限定名称,如
&CA1::binarySearch
,以及绑定将调用其方法的
CA1
实例,例如
std::bind(&CA1::binarySearch,this,vectornordered,2)
是的,我需要在binarySearch中访问类的私有成员,我已经更改了代码,但现在我遇到了一个新错误:请参阅正在编译的函数模板实例化'std::function::function(_Fx&&')。我必须为赋值编写binarySearch。
DoTests
现在看起来怎么样?void CA1::DoTests(){auto time=measure(std::bind(&CA1::binarySearch,this,vectorUnordered,2));}这似乎是正确的。与您所做的类似吗?是的,我需要在binarySearch中访问类的私有成员,我更改了代码,但现在我遇到一个新错误:请参阅函数模板实例化'std::function::function(_Fx&&&)'正在编译我必须为赋值编写binarySearch。
DoTests
现在是什么样子?void CA1::DoTests(){auto time=measure(std::bind(&CA1::binarySearch,this,vectorUnordered,2));}这似乎是正确的。与您所做的类似吗?