如何使用double[];初始化boost::random::discrete_distribution;

How to initialize boost::random::discrete_distribution using double[];

本文关键字:random discrete distribution boost 初始化 何使用 double      更新时间:2023-10-16

我想用一个双[]初始化boost::random::discrete_distribution,如下所示:

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

我知道我可以使用向量或静态大小的表,但有没有一种方法可以在不重写_distr的情况下克服这一点?

discrete_distribution<>不能接受普通的double*参数,因为它无法知道数组的长度。

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

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

和往常一样,这一点在文档中非常清楚。