我知道我们可以使用以下代码来打印数组中的元素,例如:
int a[] = {1,2,3,4,5};
for (int el : a) {
cout << el << endl;
}
但是如果我们的数组有两个或多个维度怎么办? 应如何修改 for 循环才能打印更高维的数组? 例如:
int b[2][3] = {{1,2,3},{3,4,5}};
谢谢:)
最佳答案
怎么样:
int b[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
for (auto& outer : b)
{
for (auto& inner : outer)
{
std::cout << inner << std::endl;
}
}
关于c++ - C++ 中的 for-each-loop 用于二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51223535/