开发者

C++: how do I assign values to a list of vectors efficiently?

开发者 https://www.devze.com 2023-02-19 05:30 出处:网络
Reading this question, I\'ve decided I need to use something like this: list<vector<开发者_Go百科;double> > psips;

Reading this question, I've decided I need to use something like this:

list<vector<开发者_Go百科;double> > psips;

for a simulation in c++. The question is, what is the simplest (and a reasonably efficient) way of initiating a list like this containing N vectors with d zeros in each?

Cheers!


std::list<std::vector<double> > psips(N, std::vector<double>(d));

See #3 here and #2 here.


you can use the stl constructor, and set the default value to zero:

explicit vector ( size_type n, const T& value= T()); explicit list ( size_type n, const T& value = T())

So what you would do is:

vector< double > example( d, 0);

list< vector < double > > your_list(N, example);

And you have a list of N vector and d vector with zeros in it.


 std::list<std::vector<double> > psips(100, std::vector<double>(10, 20.0));

Each vector in the list is having 10 elements, each initialized with 20.0. And total number of such vectors in the list is 100.

0

精彩评论

暂无评论...
验证码 换一张
取 消