Return Boost distribution generator from function to populate vectors in C++

后端 未结 1 642
感情败类
感情败类 2021-01-26 11:54

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         


        
相关标签:
1条回答
  • 2021-01-26 12:13

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

    Live On 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);
    }
    
    0 讨论(0)
提交回复
热议问题