explorer
explorer

Reputation: 313

How to push_back values in the middle of a row of 2D array using std::vector?

I am new to std::vector and my problem may have a very simple solution but i am not aware. For this code,

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    vector <vector<int> > grid;
    vector<int> col;
    col.push_back(1);
    for(int i = 0; i < 5; i++){
         grid.push_back(col);
    }
return 0;
}  

This is will make the fist column of grid as 1. Like this:

1
1
1
1
1  

Is there a way by which i can go to the second last row and add a few zeros there? Like this?

1
1
1
1 0 0 0
1  

Upvotes: 0

Views: 619

Answers (1)

Emil Laine
Emil Laine

Reputation: 42848

grid.at(3) gives you the 4th element in grid, i.e. your second last row. grid.at(3).push_back(...) will add to it.

Upvotes: 2

Related Questions