How to set a vector of “discrete_distribution” c++

2019-02-23 01:37发布

I'm trying to make a simulation of something like a Markov chain and using discrete_distribution to simulate the change of state 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 this doesn't work.

note: if I try just 1 vector, this is a vector of uint16_t works

// 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()) );

is not correct, but I say about the change v1 to v. to demonstrate that is possible use a vector of discrete distributions

2条回答
Emotional °昔
2楼-- · 2019-02-23 01:47

I find a way to do it like 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 emptied vectors of distributions

std::vector<std::discrete_distribution <uint64_t>> distr;
  set_distributions(distr, vecs);
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-23 01:53

You can use list initialization to initialize nested vectors. E.g.:

std::vector<std::vector<int>> v{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
查看更多
登录 后发表回答