c++ - 打印多维数组中的元素 - C++

标签 c++ arrays

帮助我使用这个 cplusplus 程序获得正确的结果。 使用这段代码,我试图打印数组“arr”中的所有元素,每个内部数组都在它自己的行上。

代码:

using namespace std;

int main()
{

    int arr[3][4] = {
        {1,2,3,4},{5,6,7,8},{9,10,11,12}
    };

    for (auto p = begin(arr); arr != end(arr); ++p) {
        for (auto q = begin(*p); q != end(*p); ++q) {

            cout << *q << " ";
        }
    }

    cout << endl;

    keep_window_open();
    return 0;

}

但是当我执行这段代码时,编译器向我显示了一堆内存地址,而不是数组“arr”中的元素。我对该代码做错了什么吗?我正在使用 Visual Studio 2015 进行编程,以备不时之需。

最佳答案

试试这个:

int ia[3][4] = {
            { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }
        };

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                cout << ia[i][j] << endl;
            }
        }

关于c++ - 打印多维数组中的元素 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296579/

相关文章:

C++ 堆错误 _CrtlsValidHeapPointer(pUserData)

c++ - 奇怪的宏扩展

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 必须插入varchar时如何将数组插入MySQL

python numpy数组切片

c++ - 多个源文件中带有外部全局变量的 Unresolved 链接器错误

c++ - 为什么这个涉及 std::enable_if 的模板元函数会产生不希望的结果?

c++ - 是否可以在 RAD Studio 10 c++ 中设置 Window (x, y) Position?

Javascript,仅加载存在的图像

c - 程序在运行时终止