简体   繁体   中英

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

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

list<vector<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));

见#3 这里和#2 在这里

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM