Initializing a vector of vectors having a fixed size with boost assign

saloua picture saloua · Oct 29, 2012 · Viewed 66.3k times · Source

Having a vector of vector with a fixed size,

vector<vector<int> > v(10);

I would like to initialize it so that it has in all elements a one dimensional vector with initialized value (for example 1).

I have used Boost Assign as follows

v= repeat(10,list_of(list_of(1)));

and I've got a compilation error

error: no matching function for call to ‘repeat(boost::assign_detail::generic_list<int>)’

Could you please tell me how to do that. Thanks in advance

Answer

juanchopanza picture juanchopanza · Oct 29, 2012

This doesn't use boost::assign but does what you need:

vector<vector<int>> v(10, vector<int>(10,1));

This creates a vector containing 10 vectors of int, each containing 10 ints.