c++ - How to create a std::initializer_list from variable data -


i'm trying create std::discrete_distribution object using data passed class constructor. know how create using static data, cannot figure out how using variable data (cleanly). have "works", painful. there more appropriate way of doing this?

the distinit = { distarray[0], ... }; line problem.

#include <iostream> #include <iomanip> #include <initializer_list> #include <map> #include <random>  class die {     private:         int loadside;         double loadamount;         std::mt19937 generator;         std::discrete_distribution<> distribution;         std::initializer_list<double> distinit;         std::array<double, 7> distarray;     public:         die( int loadside, double loadamount ) : loadside(loadside), loadamount(loadamount) {             distarray.fill( 1 );             distarray[0] = 0;             distarray[this->loadside] = this->loadamount;              distinit = { distarray[0], distarray[1], distarray[2], distarray[3], distarray[4], distarray[5], distarray[6] };             distribution.param( distinit );         };         int roll( ) {                 return distribution( generator );         }; };  const int rounds = 10000;  int main() {     die* die = new die( 5, 20 );      std::map<int, int> m;     for(int n=0; n < rounds; n++) {         m[die->roll()]++;     }     for(auto p : m) {         std::cout << p.first << " generated " << std::setiosflags(std::ios::fixed) << std::setprecision(2) << (float) p.second / rounds << " times\n";     } } 

i may not asking right question, apologize in advance if so. strong possibility i'm surprised i'm unable find (apparently) related hits on subject.

my compiler g++-mp-4.8 (macports gcc48 4.8-20130411_0) 4.8.1 20130411 (prerelease)

command line /opt/local/bin/g++-mp-4.8 -std=c++11 test.cpp -o test

if have variable data, should using discrete_distribution constructor taking pair of iterators:

template< class inputit > discrete_distribution( inputit first, inputit last ); 

you shouldn't trying construct param_type directly; instead use helper function construct distribution:

class die {     private:         std::mt19937 generator;         std::discrete_distribution<> distribution;         static std::discrete_distribution<> makedistribution(             int loadside, double loadamount )         {             std::array<double, 7> distarray;             distarray.fill( 1 );             distarray[0] = 0;             distarray[loadside] = loadamount;             return {std::begin(distarray), std::end(distarray)};         }     public:         die( int loadside, double loadamount ) :             generator{ },             distribution{ makedistribution( loadside, loadamount ) }         {}         int roll( ) {                 return distribution( generator );         } }; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -