简体   繁体   中英

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  

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM