从函数返回Boost分布生成器以在C ++中填充向量 [英] Return Boost distribution generator from function to populate vectors in C++

查看:76
本文介绍了从函数返回Boost分布生成器以在C ++中填充向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下应返回分布生成器的函数,以便可以使用该函数填充向量,即

I have the following function that should return a distribution generateor such that I can use the function to populate a vector i.e.

template<typename T3>
T3 new_func()
{
    std::uniform_real_distribution<> Uni(1,2);
    boost::variate_generator< boost::mt19937, std::uniform_real_distribution<>> Uniform(gen , Uni);
    return Uniform;
}

int main ()
{
    std::vector<double> test;
    //something like
    std::for_each(test.begin(), test.end(),
         [&](std::vector<double>& a){a.push_back(new_func());});
}

我特别需要此功能,因为我将有一个参数化函数来生成要以这种方式使用的多个分布.请建议 我到底需要做些什么来实现这一目标.

I specifically need this ability as I will have a single parameterized function to generate multiple distributions to be used in this fashion. Kindly suggest what exactly I need to do achieve this.

推荐答案

您可以使用boost::function<>(甚至是std::function<>):

You can use boost::function<> (or even std::function<>):

在Coliru上直播

#include <boost/math/distributions.hpp>
#include <boost/function.hpp>
#include <boost/random.hpp>
#include <random>

static boost::mt19937 gen;
typedef boost::function<double()> Distri;

Distri new_func()
{
    std::uniform_real_distribution<> Uni(1,2);
    return boost::variate_generator< boost::mt19937, std::uniform_real_distribution<>>(gen , Uni);
}

int main ()
{
    std::vector<double> test;
    //something like
    Distri d = new_func();

    //likely
    std::generate_n(back_inserter(test), 100, d);
}

这篇关于从函数返回Boost分布生成器以在C ++中填充向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆