小编Chr*_*ris的帖子

如何使用boost的brent_find_minima等算法构造一个仿函数?

我试图理解为什么带有构造函数的仿函数不能传递给算法,而没有构造函数的仿函数可以.

对于算法boost-brent_minima.当函子没有构造函数时,示例代码工作正常:

#include <boost/math/tools/minima.hpp>

struct funcdouble
{
  double operator()(double const& x)
  { //
    return (x + 3) * (x - 1) * (x - 1); // (x + 3)(x - 1)^2
  }
};

int bits = std::numeric_limits<double>::digits;

std::pair<double, double> r = brent_find_minima(funcdouble(), -4., 4. / 3, bits);

std::cout.precision(std::numeric_limits<double>::digits10);
std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second << std::endl;
Run Code Online (Sandbox Code Playgroud)

但是,当我使用构造函数生成这样的自定义函子时:

struct solver_test{

    solver_test::solver_test(std::string expression_string, std::string x_name_) : x_name(x_name_){
        symbol_table.add_constants(); …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm constructor boost functor

6
推荐指数
1
解决办法
89
查看次数

标签 统计

algorithm ×1

boost ×1

c++ ×1

constructor ×1

functor ×1