如何将3D数组转换为1D数组? [英] How to convert a 3-D array into 1-D array?

查看:182
本文介绍了如何将3D数组转换为1D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将3D数组转换为1D数组?

感谢

How to convert a 3-D array into 1-D array?

thanks

推荐答案

一个3D数组同时是一个1D数组.要了解它,请看这里: http://www.cplusplus.com/doc/tutorial/arrays/ [^ ].

本文中的示例:
A 3D array is a 1D array at the same time. To understand it, look here: http://www.cplusplus.com/doc/tutorial/arrays/[^].

A sample from this article:
//2D or 3D -- no difference [SA]:
int jimmy [3][5];   // is equivalent to
int jimmy [15];     // (3 * 5 = 15)



这样,jimmy[2,4]jimmy[14]相同.但是,您需要知道索引的顺序.您将从同一篇文章中弄清楚;但是尝试清晰的图片也是一件好事,实际上很容易.

找到多维数组"部分以获取更多详细说明.

—SA



In this way, jimmy[2,4] is the same as jimmy[14]. You need to know the order of indices though. You will figure it out from the same article; but it''s also good to experiment to have a clear picture, which is in fact easy.

Locate the section "Multidimensional arrays" for more detailed explanations.

—SA


int
main(int argc, char** argv)
{

    int array[3][3][3];
    int a[27];

    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<3;k++)
            {
                array[i][j][k]= i*(9)+j*(3)+k;
            }
        }
    }
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<3;k++)
            {
                a[i*(9)+j*(3)+k]=array[i][j][k];
            }

        }
    }

    for(int i=0;i<27;i++)
        cout<<a[i];
    return 0;


}


这篇关于如何将3D数组转换为1D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆