trolle3000
trolle3000

Reputation: 1067

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!

Upvotes: 2

Views: 3586

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361362

 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.

Upvotes: 1

0x26res
0x26res

Reputation: 13902

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.

Upvotes: 2

ildjarn
ildjarn

Reputation: 62975

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

See #3 here and #2 here.

Upvotes: 4

Related Questions