Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++_Function Pointers - Fatal编程技术网

C++ C+中的函数指针+;二等分法

C++ C+中的函数指针+;二等分法,c++,function-pointers,C++,Function Pointers,我有一个函数my_func(),它接受两个参数a和b 我想在solve\u for_b_by_bisection()函数中定义一个名为f的函数,这样我就可以调用f(b),对于某些固定输入a,它就是my_func(a,b)。我该怎么做?我是否使用函数指针 我之所以这样做而不是直接调用f(a,b),是因为在我正在处理的实际问题中,它有10个以上的变量,这些变量都是常量——不可能每次都重复变量列表 double my_func(const double a, const double b) {

我有一个函数
my_func()
,它接受两个参数
a
b

我想在
solve\u for_b_by_bisection()
函数中定义一个名为
f
的函数,这样我就可以调用
f(b)
,对于某些固定输入
a
,它就是
my_func(a,b)
。我该怎么做?我是否使用函数指针

我之所以这样做而不是直接调用
f(a,b)
,是因为在我正在处理的实际问题中,它有10个以上的变量,这些变量都是常量——不可能每次都重复变量列表

double my_func(const double a, const double b)
{
    /* some arbitary function */ 
}

double solve_for_b_for_contant_a_by_bisection (const double a, 
                                               const double upperbound, 
                                               const double lowerbound)
{
    double (*my_func_pointer)(const double b)
    {
        &my_func(a, b)
    }
             
    lowerboundvalue = *my_func(lowerbound)
    upperboundvalue = *my_func(upperbound)
    midpointvalue = *my_func(0.5 * (lowerbound+upperbound))
    /* The rest of the implementation */
}
您可以使用:

只需使用lambda:

double solve_for_b_for_contant_a_by_bisection (const double a, 
                                               const double upperbound, 
                                               const double lowerbound)
{
         auto f = [a]( double b ) { return my_func( a, b ); };
         
         auto lowerboundvalue = f(lowerbound)
         auto upperboundvalue = f(upperbound)
         auto midpointvalue = f(0.5 * (lowerbound+upperbound));
         /* The rest of the implementation */
     
}

您可以按照其他人的建议使用lambda函数,也可以使用
std::bind
。看看它会是什么样子,你是否更喜欢它:

#include <functional>

double my_func(const double a, const double b)
{
    /* some arbitary function */ 
}

double solve_for_b_for_contant_a_by_bisection (const double a, 
                                               const double upperbound, 
                                               const double lowerbound)
{
    const auto f = std::bind(my_func, a, std::placeholders::_1);
             
    const auto lowerboundvalue = f(lowerbound);
    const auto upperboundvalue = f(upperbound);
    const auto midpointvalue = f(0.5 * (lowerbound+upperbound));
    /* The rest of the implementation */
}
#包括
加倍我的函数(常数加倍a,常数加倍b)
{
/*一些任意函数*/
}
通过对分(常数双a,
常量双上界,
常量双下位键)
{
const auto f=std::bind(my_func,a,std::占位符::_1);
const auto lowerboundvalue=f(lowerbound);
常量自动上限值=f(上限);
常量自动中点值=f(0.5*(下限+上限));
/*实施的其余部分*/
}

它被称为一个闭包。如果我有一个变量列表而不是一个,那会怎么样?@Lost1:你可以捕获更多的变量,或者显式地
[a]
/
/
[&a]
,或者通过值
[a]
/
[=],如何用参数列表替换参数?只需执行自动f=[a,c](双b){返回我的函数(a,b,c);};?
#include <functional>

double my_func(const double a, const double b)
{
    /* some arbitary function */ 
}

double solve_for_b_for_contant_a_by_bisection (const double a, 
                                               const double upperbound, 
                                               const double lowerbound)
{
    const auto f = std::bind(my_func, a, std::placeholders::_1);
             
    const auto lowerboundvalue = f(lowerbound);
    const auto upperboundvalue = f(upperbound);
    const auto midpointvalue = f(0.5 * (lowerbound+upperbound));
    /* The rest of the implementation */
}