vendredi 15 mai 2020

c++17, initialise an array

I'm (re)learning c++ and want to initialise an array of objects.

struct Pea{
    Pea(double lower, double upper){
        static std::default_random_engine generator;
        static std::uniform_real_distribution<double> distribution(lower,upper);
        weight = distribution(generator);
    }
    double weight;
};

class Items{
    std::array<Pea,10> peas();
public:
    Items(){

    }
    void show(){
        std::for_each(begin(peas),end(peas),
                      [](auto pea){
                          std::cout << pea.weight << std::endl;

                      });
    }
};

The example is a big silly but it's just for learning. I want to initialise the array 'peas' with random weights. But I want to specify the lower and upper limits of random.

The line std::array peas(1.2,2.3); does not compile, as expected, can anyone suggest a 'modern' way of doing this.

Thanks

Aucun commentaire:

Enregistrer un commentaire