如何沿行组合两个向量向量?
How to combine two vector of vector's along the row?
我正在尝试将一个 向量的向量 附加到另一个 向量的向量 沿行,就像 4x4
向量变为 4x8
(不是 8x4
row/column)。
我需要指示才能完成 4x8
。
Grid
是一个包含 Square
个对象的对象。
Grid CombineGrid(Grid one, Grid two)
{
vector<vector<Square>> gridOne = one.fields;
vector<vector<Square>> gridTwo = two.fields;
vector<vector<Square>> temp;
int reqCol = one.columns + two.columns, reqRow = one.rows + one.rows;
temp = gridOne;
temp.insert(temp.end(), gridTwo.begin(), gridTwo.end());
for (const auto &row : temp)
{
for (Square x:row)
{
cout << "y" << ' ';
cout << endl;
}
}
Grid finalGrid(one.columns + two.columns, two.rows + two.rows);
finalGrid.fields = temp;
return finalGrid;
}
是否要将 gridOne[0](第一个 vect 的第一个元素)与 gridTwo[0](第二个 vect 的第一个元素)以及其他元素连接起来?
如果答案是肯定的,你可以尝试这样的事情:
size_t grid1Size = gridOne.size();
for (size_t i = 0; i < grid1Size; i++)
{
if(i > gridTwo.size())
{
gridTwo.push_back(gridOne[i]);
}
else
{
gridTwo[i].insert(gridTwo[i].end(), gridOne[i].begin(), gridOne[i].end() );
}
}
我正在尝试将一个 向量的向量 附加到另一个 向量的向量 沿行,就像 4x4
向量变为 4x8
(不是 8x4
row/column)。
我需要指示才能完成 4x8
。
Grid
是一个包含 Square
个对象的对象。
Grid CombineGrid(Grid one, Grid two)
{
vector<vector<Square>> gridOne = one.fields;
vector<vector<Square>> gridTwo = two.fields;
vector<vector<Square>> temp;
int reqCol = one.columns + two.columns, reqRow = one.rows + one.rows;
temp = gridOne;
temp.insert(temp.end(), gridTwo.begin(), gridTwo.end());
for (const auto &row : temp)
{
for (Square x:row)
{
cout << "y" << ' ';
cout << endl;
}
}
Grid finalGrid(one.columns + two.columns, two.rows + two.rows);
finalGrid.fields = temp;
return finalGrid;
}
是否要将 gridOne[0](第一个 vect 的第一个元素)与 gridTwo[0](第二个 vect 的第一个元素)以及其他元素连接起来? 如果答案是肯定的,你可以尝试这样的事情:
size_t grid1Size = gridOne.size();
for (size_t i = 0; i < grid1Size; i++)
{
if(i > gridTwo.size())
{
gridTwo.push_back(gridOne[i]);
}
else
{
gridTwo[i].insert(gridTwo[i].end(), gridOne[i].begin(), gridOne[i].end() );
}
}