二分法中 C++ 中的函数指针
function pointer in C++ in bisection method
我有一个函数 my_func()
,它有两个参数 a
和 b
。
我想在 solve_for_b_by_bisection()
函数中定义一个函数,称为 f
,这样我就可以调用 f(b)
,对于某些固定输入,它是 my_func(a, b)
a
。我怎么做?我使用函数指针吗?
我这样做而不是直接调用 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:
auto func = [a](double b) { return my_func(a, b); };
只需使用 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 */
}
我有一个函数 my_func()
,它有两个参数 a
和 b
。
我想在 solve_for_b_by_bisection()
函数中定义一个函数,称为 f
,这样我就可以调用 f(b)
,对于某些固定输入,它是 my_func(a, b)
a
。我怎么做?我使用函数指针吗?
我这样做而不是直接调用 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:
auto func = [a](double b) { return my_func(a, b); };
只需使用 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 */
}