Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How does a multi-dimensional vector grow internally?

Tags:

c++

vector

matrix

Consider a two-dimensional vector with integers, but initially only with one element:

std::vector< std::vector <int> > vec( 1, std::vector<int>( 1, 0 ) );

Now I want to make the vector in both dimensions larger, so that the vector results always in a m x m matrix.

Will the following two commands do that:

vec.push_back( std::vector<int> );
vec[0].push_back( 0 );

or will just the first row and respectively the first column increase by an element?

like image 216
fotinsky Avatar asked Apr 13 '26 14:04

fotinsky


2 Answers

Only the first row will enlarge. The fact the vectors are all grouped together doesn't make them enlarge together.

like image 131
zneak Avatar answered Apr 15 '26 02:04

zneak


If you are creating a vector of vectors, you will need to increase every row if you want the width of every row to increase....

like image 31
v010dya Avatar answered Apr 15 '26 04:04

v010dya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!