Вопрос

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