# Add element to two demension Vector with no fixed size.

• 11-09-2011
hqt
Add element to two demension Vector with no fixed size.
My question is very easy if this vector has a fixed size. for example:
Code:

```vector< vector<int> > matrix(5,vector<int>(5,0)); matrix[4][4]=2; matrix[3][2]=1;```
But that is not obvious when you don't fixed size for vector. You cannot use push_back like one demension array. This is my example:
Code:

```  vector< vector<int> >matrix;   vector<int>  array;   array.push_back(5); //OK   matrix.push_back(5); //NOT OK. So How can.```
So, that's my problem. Who know how to code it, help me please.
thanks for ton :)
• 11-09-2011
Elkvis
when adding items to matrix, you have to push_back a vector<int>, not just an int. so in your example, you might say matrix.push_back(array) instead of matrix.push_back(5).
• 11-09-2011
jimblumberg
Code:

```array.push_back(5); //OK matrix.push_back(array);```
Jim
• 11-09-2011
hqt
Oh, thanks for yours help :) But, I think above solution has a weak point: you must process array vector first before you add it too matrix. So, two demension vector will not as flexible as two demension array. huh :-? ;)

thanks :)
• 11-09-2011
laserlight
Quote:

Originally Posted by hqt
But, I think above solution has a weak point: you must process array vector first before you add it too matrix. So, two demension vector will not as flexible as two demension array. huh :-?

It is you who chose to create the vector named array (which is a poor name given std::array). I can choose otherwise:
Code:

```vector< vector<int> > matrix; matrix.push_back(vector<int>(1, 5));```
That said, if you have more elements for the inner vector that arbitrarily differ in value, then you may need to do more than just the above.
• 11-09-2011
Elysia
Actually, the problem arises from the fact that you do not specify whether you want to push back a new column or a new row.
What does matrix.push_back(5) mean? Row? Or column?