【问题标题】:How to initialize boost::random::discrete_distribution using double[];如何使用 double[] 初始化 boost::random::discrete_distribution;
【发布时间】:2012-03-29 21:37:09
【问题描述】:

我想用这样的 double[] 初始化 boost::random::discrete_distribution:

boost::random::discrete_distribution<>* distribution(double* _distr)
{
    return new boost::random::discrete_distribution<>(_distr);
}

我知道我可以使用矢量或静态大小的表格,但有没有办法在不重写我的 _distr 的情况下克服它?

【问题讨论】:

  • 感谢您拥有我见过的最大的 Gravatar。我不知道有可能做这样的事情。

标签: c++ boost boost-random


【解决方案1】:

discrete_distribution&lt;&gt; 不能采用普通的 double* 参数,因为它无法知道数组的长度。

它需要一个迭代器范围,但您必须指定数组中的元素数量:

boost::random::discrete_distribution<>* distribution(double const* distr,
                                                     std::ptrdiff_t count)
{
    return new boost::random::discrete_distribution<>(distr, distr + count);
}

像往常一样,这在the documentation 中非常清楚。

【讨论】:

  • 谢谢!这就是我所希望的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
相关资源
最近更新 更多