I would like to initialize boost::random::discrete_distribution with a double[] like this:

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

I know I can use vector or statically sized table but is there a way to overcome that without rewriting my _distr?

有帮助吗?

解决方案

discrete_distribution<> cannot take a plain double* argument because it would have no way to know how long the array is.

Instead it takes an iterator range, but you'll have to specify the number of elements in your array:

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

As usual, this is made quite clear in the documentation.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top