• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++动态多维数组范例

OC/C/C++ 水墨上仙 1271次浏览

C++动态多维数组范例

//creation of dynamic multidimensional arrays?
#include <iostream>
#include <iomanip>
using namespace std;
 
int main()
{
    //For creating one dimension array of 4 integers
    int *ptr1;
    ptr1 = new int[4];
    for( int i = 0 ; i < 4 ; i++)
    {
        ptr1[i] = i+1;
    }
 
    cout << "\nOne dimension array of 4 integers:\n";
    for( int i = 0 ; i < 4 ; i++)
    {
        cout << "  " << ptr1[i] ;
    }
 
    cout << endl << endl;
 
    //for creating two dimensional array of 3 by 4 integers
    int **ptr2;
    ptr2 = new int*[3];
    for( int i = 0 ; i < 3 ; i++)
    {
        ptr2[i] = new int[4];
    }
 
    for( int i = 0 ; i < 3 ; i++)
    {
        for ( int j = 0 ; j < 4 ; j++)
        {
            ptr2[i][j] = i * 4 + j;
        }
    }
 
    cout << "\nTwo dimensions array of 3 by 4 integers:\n";
    for( int i = 0 ; i < 3 ; i++)
    {
        for ( int j = 0 ; j < 4 ; j++)
        {
            cout << setw(4) << right << ptr2[i][j] << " ";
        }
        cout << endl;
    }
 
    //for creating three dimensional array of 2 by 3 by 4 double float
    double ***ptr3;
    ptr3 = new double**[2];
    for( int i = 0 ; i < 2 ; i++)
    {
        ptr3[i] = new  double*[3];
    }
    for( int i = 0 ; i < 2 ; i++)
    {
        for( int j = 0 ; j < 3 ; j++)
        {
            ptr3[i][j] = new  double[4];
        }
    }
 
    for( int i = 0 ; i < 2 ; i++)
    {
        for( int j = 0 ; j < 3 ; j++)
        {
            for( int k = 0 ; k < 4 ; k++)
            {
                ptr3[i][j][k] = ( i * 3 + j ) * 4.1 + k * 0.1;
            }
        }
    }
    cout << "\nThree dimensions array of 2 by 3 by 4 double float :\n";
    for( int i = 0 ; i < 2 ; i++)
    {
        for( int j = 0 ; j < 3 ; j++)
        {   
            for( int k = 0 ; k < 4 ; k++)
            {
                cout <<"\nptr3(" << i << "," << j << "," << k << ") = " << setprecision(4) <<
                {
                    ptr3[i][j][k];
                }
            }
        }
    }
 
    return 0;
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++动态多维数组范例
喜欢 (0)
加载中……