排列 2D 数组中的元素
Permute an element in 2d-array
我正在尝试通过在我的程序 2D 数组中使用来移动矩阵的零以获得它的未成年人。如何正确删除 2D 数组的(移位(元素?
我知道如何通过排列其元素来处理 1d 数组的这个问题
for (int i = DEL; i < (SIZE - 1); i++)
array[i] = array[i + 1];
其中 DEL - 元素的索引,我们想要删除,SIZE - 数组的大小。但是我对多维数组的结果并不相同:
for (int i = DEL; i < (SIZE - 1); i++)
for (int j = DEL; j < (SIZE - 1); j++)
array[i][j] = array[i+1][j+1];
哪里有错误?
#include <iostream>
using namespace std;
int main()
{
int array[3][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9};
// Setting to zero 2nd row and 3rd column
int m = 1; // 2
int n = 2; // 3
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
array[m][j] = 0;
array[i][n] = 0;
cout << array[i][j];
// Trying to shift every element, which equals zero
if (array[i][j] == 0)
for (int k = i; k < 2; k++)
for (int l = j; l < 2; l++)
array[k][l] = array[k+1][l+1];
}
cout << endl;
}
cout << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
cout << array[i][j];
cout << endl;
}
}
我得到:
120
000
780
120
000
780
但实际上我希望最后一个输出是这样的:
120
780
000
我认为术语"排列"不适合这里。您想要从二维数组中删除行和列。
但是,您犯了一些语义错误。我为您修复了这个问题,并在下面向您展示了更正后的代码。
一个很大的建议。尝试将一个大问题缩小为几个小问题。您在多嵌套 for 循环中尝试了太多。一个接一个地做。
请看:
#include <iostream>
constexpr size_t MaxArraySize = 3;
int main()
{
int array[MaxArraySize][MaxArraySize] = {
1, 2, 3,
4, 5, 6,
7, 8, 9 };
// Setting to zero 2nd row and 3rd column
int rowToDelete = 1; // 2
int colToDelete = 2; // 3
// Set cell to delete to 0
for (int row = 0; row < MaxArraySize; ++row)
{
for (int col = 0; col < MaxArraySize; ++col)
{
array[rowToDelete][col] = 0;
array[row][colToDelete] = 0;
std::cout << array[row][col];
}
std::cout << 'n';
}
// First shift all rows
for (int row = rowToDelete; row < MaxArraySize - 1; ++row)
for (int col = 0; col < MaxArraySize; ++col)
array[row][col] = array[row+1][col];
// Then shift all cols
for (int col = colToDelete; col < MaxArraySize-1; ++col)
for (int row = 0; row < MaxArraySize; ++row)
array[row][col] = array[row][col+1];
// Set the cells that were shifted away to 0
for (int row = 0; row < MaxArraySize; ++row)
array[row][MaxArraySize - 1] = 0;
for (int col = 0; col < MaxArraySize; ++col)
array[MaxArraySize - 1][col] = 0;
// Show result
std::cout << "nnResultnn";
for (int row = 0; row < MaxArraySize; ++row)
{
for (int col = 0; col < MaxArraySize; ++col)
std::cout << array[row][col];
std::cout << 'n';
}
}
第二,甚至更重要。您正在使用纯 C 代码和纯 C 样式数组。代码中唯一的C++是使用 iostreams。那不太好。
尝试使用C++。永远不要使用普通的 C 样式数组。尝试使用 STL 中的算法和容器。为变量使用更好的名称。
生成的代码段将非常简单。在这里,我们实际上是在删除行和列。请看:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
// Lambda for printing the matric to std::cout
auto print = [](std::vector<std::vector<int>> & m) {
std::for_each(m.begin(), m.end(), [](std::vector<int> & vi) { std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << 'n'; });
};
// You can add whatever values.
std::vector<std::vector<int>> matrix {
{11, 12, 13, 14},
{15, 16, 17, 18},
{19, 20, 21, 22},
{23, 24, 25, 26}
};
// Show initial data
std::cout << "nnInitial matrix:n";
print(matrix);
constexpr size_t RowToDelete = 1; // Row 2
constexpr size_t ColToDelete = 2; // Column3
// Erase row
matrix.erase(matrix.begin() + RowToDelete);
// Erase column in each row
std::for_each(matrix.begin(),matrix.end(), [ColToDelete](std::vector<int> & vi) { vi.erase(vi.begin() + ColToDelete); });
// Show result
std::cout << "nnResulting matrix with deleted row " << RowToDelete+1 << " and deleted column " << ColToDelete+1 << 'n';
print(matrix);
return 0;
}
希望这有帮助。 。 。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 如何将元素添加到数组的线程安全函数?
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 输出没有重复元素的动态数组(收缩数组)C++
- 数组中最大的非重复元素
- 数组元素打印的递归方法
- 对字符数组中的元素执行逐位操作
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过交换元素使数组相同
- 按平均值替换数组中的元素
- 打印矢量数组中的所有元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 如何将元素从向量转移到新数组?
- 如何在向量数组中插入元素?
- 为什么 2 个相同数组的元素彼此相等
- C++ 中的二维整数数组,每行中的元素数量不均匀