How to resize 2d vector

Viewed 363
#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
    
    vector<vector<int> > matrix;
    
    matrix.resize(3, vector<int>(4, 1));

    for (size_t i = 0; i < 3; i++)
    {
        for (size_t j = 0; j < 4; j++)
        {
            cout << matrix[i][j];
        }
        cout << endl;
    }
    
    matrix.resize(5, vector<int>(7, 0));

    for (size_t i = 0; i < 5; i++)
    {
        for (size_t j = 0; j < 7; j++)
        {
            cout << matrix[i][j];
        }
        cout << endl;
    }

    return 0;
}

'''

As far as I know, when we are resizing a vector using "resize()" over than original capacity, values in original space will remain and new values are assigned to new space.

In the line matrix.resize(5, vector(7, 0)); If we execute that line I thought matrix would be like

1111000 
1111000 
1111000 
0000000 
0000000

something like this.

But the programs stops,

enter image description here

I want to know why it won't working.

3 Answers

i tested your codes using an online compiler https://onlinegdb.com/BkwcGuAAD

your columns are not resized (only the rows are resized). running your current code yields

1 1 1 1 0 0 33                                                                                                                                   
1 1 1 1 0 0 33                                                                                                                                  
1 1 1 1 0 0 49     << notice some columns have random numbers?                                                                                                                            
0 0 0 0 0 0 0                                                                                                                                   
0 0 0 0 0 0 0   

try resizing the columns too

matrix[0].resize(7,0);
matrix[1].resize(7,0);
matrix[2].resize(7,0);

matrix.resize(5, vector<int>(7, 0));

you should get something like the following

1 1 1 1 0 0 0                                                                                                                                 
1 1 1 1 0 0 0                                                                                                                                 
1 1 1 1 0 0 0                                                                                                                                 
0 0 0 0 0 0 0                                                                                                                                 
0 0 0 0 0 0 0                                                                                                                                 
               
matrix.resize(5, vector<int>(7, 0));

only add new vector of size 7 not modifying actual vector. Just resize actual vectors to 7 with:

for (auto &row: matrix) row.resize(7);

so now is working:

1111000                                                                                                                                                                                     
1111000                                                                                                                                                                                     
1111000                                                                                                                                                                                     
0000000                                                                                                                                                                                     
0000000

By the C++ Reference (http://www.cplusplus.com/reference/vector/vector/resize/)

void resize (size_type n);
void resize (size_type n, const value_type& val);

Resizes the container so that it contains n elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.


I thought Compiler will understand if I put more longer vector inside of "val".

That is, I thought it would understand this kind of increased "n".

But Compiler will only watch whether "n" parameter itself is changed or not.

Because of this reason, my code wouldn't work properly.

if you want to increase size of vector using size() function, note that you should resize original row values on your hand.

Related