How to set C ++ discrete_distribution vector

I am trying to simulate something like a Markov chain and using a discrete distribution to simulate a change in the state of s_i to s_j. But, of course, this is a Matrix, not a vector. So I try.

std::vector <uint16_t> v {{...}, {...}, ... {...},}; std::vector <std::discrete_distribution <uint16_t> > distr(n, std::distribution <uint16_t> (v.begin(), v.end()) ); 

but it does not work.

note: if i try only 1 vector, this is uint16_t work vector

 // CHANGE v by v[0] std::vector<std::discrete_distribution <uint64_t>> distr(1, std::discrete_distribution <uint64_t> (vecs[0].begin(), vecs[0].end())); 

based on this answer

I know that

 std::vector <std::discrete_distribution <uint16_t> > distr(n, std::distribution <uint16_t> (v.begin(), v.end()) ); 

wrong, but I'm talking about changing v1-v. To demonstrate that it is possible to use a vector of discrete distributions

+5
source share
2 answers

You can use list initialization to initialize nested vectors. For instance:.

 std::vector<std::vector<int>> v{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 
+5
source

I find a way to do this, as in this answer

using this template

 template<typename T> void set_distributions(std::vector< std::discrete_distribution <T> > &distr, const std::vector< std::vector<T> > &vecs){ for (uint64_t i = 0; i < vecs.size(); ++i) { distr.push_back( std::discrete_distribution <uint64_t> (vecs[i].begin(), vecs[i].end()) ); } } 

and with this function, when you have omitted distribution vectors

 std::vector<std::discrete_distribution <uint64_t>> distr; set_distributions(distr, vecs); 
+2
source

Source: https://habr.com/ru/post/900144/


All Articles