将开发过程中比较重要的代码备份一下,下面的代码内容是关于C++动态多维数组范例的代码,希望对小伙伴们有所用。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
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( 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++)
{
}
}
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( int i = 0 ; i < 2 ; i++)
{
}
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++)
{
}
}
}
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;
}
网友评论