I wonder if anyone can help me with this - I'm trying to create a 2D vector.
So in a header file I have: std::vector< std::vector< float > > item;
What would I then put in the cpp file to make sure it is initialised, before changing the size to what I need using resize, so that it becomes, for example, a 2x2 vector of 0s?
std::vector< std::vector< float > > item ( 2, std::vector<float> ( 2, 0 ) );
The first argument for the vector constructor is the number of elements, the second, the value used to fill the vector http://www.cplusplus.com/reference/stl/vector/vector/
Thanks, but I'm not sure how to use that for what I want... Is it possible just to define the vector as a 2-D vector in the header file and then to initialise it in the cpp file? I could I suppose define it like that in the header file (could I?) and then modify it as needed in the cpp file, but I'd rather not. I've tried doing:
item (2, std::vector<float> (2,0))
but it doesn't like that. So if anyone knows how to do this it'd be really helpful.
J
This is the way that I work with vector of vectors:
1 2 3 4 5 6 7 8
int num_of_col = 5;
int num_of_row = 9;
double init_value = 3.14;
vector< vector<double> > matrix;
//now we have an empty 2D-matrix of size (0,0). Resizing it with one single command:
matrix.resize( num_of col , vector<double>( num_of_row , init_value ) );
// and we are good to go ...