![](/img/trans.png)
[英]How to specify a range for begin() and end() iterators to find first value of sub vector in a vector of vectors?
[英]How to add a new row at the begin of a vector of vectors
我想在向量向量的開頭添加一個新行。 我有一個 3x3 向量,我想要一個 4x3 。 這是我的代碼:
typedef vector<int> state;
typedef vector<vector<int> > board;
int val;
string s;
while (getline (in, s)) {
istringstream iss (s);
while (iss >> val)
boards.push_back(val);
}
sizeb = boards.front();
for (auto it = boards.cbegin()+1; it <= boards.cbegin()+pow(sizeb, 2); ++it)
sstart.push_back(*it);
start.resize(sizeb);
for (int i = 0; i < sizeb; ++i)
start[i].resize(sizeb);
for (int i = 0; i < sizeb; i++)
for (int j = 0; j < sizeb; j++)
start[i][j] = sstart[(i * sizeb) + j];
當我嘗試做 sizeb+1 時,它會添加一個新行,但也會添加一個新列。
你可以幫幫我嗎?
如何在向量向量的開頭添加新行
你可以用std::vector::insert
做到這一點。
當我嘗試做 sizeb+1 時,它會添加一個新行,但也會添加一個新列。
那是因為您使用循環調整每一行的大小:
// I'm assuming that start is the vector of vectors
start.resize(sizeb); // this changes the number of rows
for (int i = 0; i < sizeb; ++i)
start[i].resize(sizeb); // these change the number of columns in each row
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.