![](/img/trans.png)
[英]push_back on 2D std::vector< std::vector<char*> > value made all the values are the same
[英]How to push_back values in the middle of a row of 2D array using std::vector?
我是std :: vector的新手,我的問題可能有一個非常簡單的解決方案,但我不知道。 對於此代碼,
#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;
}
這將使網格的第一列為1。如下所示:
1
1
1
1
1
有沒有一種方法可以轉到第二行並在其中添加幾個零? 像這樣?
1
1
1
1 0 0 0
1
grid.at(3)
為您提供grid
的第4個元素,即倒數第二行。 grid.at(3).push_back(...)
將添加到其中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.