c++ - C++ 中的 for-each-loop 用于二维数组

标签 c++ arrays for-loop syntax

我知道我们可以使用以下代码来打印数组中的元素,例如:

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/

相关文章:

c++ - 为什么这个 shared_ptr 在超出范围时抛出断言?

Javascript数组访问外部函数

arrays - 数据结构的选择

bash - 在 Mac 终端使用 for 循环打印每个文件名的字符数

python - 使用 "-"合并数字 (python)

c++ - 为什么删除分配的数组会导致内存错误?

c++ - 'CObject' : base class undefined but I'm including afx. h、afxwin.h 等?

c++ - 实现嵌套在模板类中的类的成员函数

c - 如何只允许在数组中输入数字?

javascript - 在 JavaScript 中的特定时间停止循环