C ++ is a powerful and widely used computer programming language. It has been around for more than 20 years since its generation and plays an important role in the development field. We will introduce one of the most important knowledge points here today, that is, Introduction to the application of C ++ dynamic arrays.
The C ++ dynamic array operation code is as follows:
- # Include <iostream>
- # Include <conio. h>
- # Include <cstdlib>
// This header file contains the function exit (). Because C ++ can recognize exit (), this header file can be omitted.
- Using namespace std;
- Void display (double ***);
- Void de_lete (double ***);
- Int x, y, z; // global variable
- Void main ()
- {
- Cout <"Enter the values of x, y, and z in the three-dimensional array data [X] [Y] [Z] respectively:" <endl;
- While (cin> x> y> z & (x <0 | y <0 | z <0 ))
- Cout <"the input is invalid. Please enter it again! "<Endl;
- Double *** data;
- Data = new double ** [x]; // creates a second-level pointer array representing the names of two-dimensional arrays that comprise a three-dimensional array
- If (data = 0 ){
- Cout <"memory allocation failed! Program termination. "<Endl;
- Exit (1 );
- }
- For (int j = 0; j <x; ++ j)
- Data [j] = new double * [y]; // create a pointer array representing the names of all one-dimensional arrays that comprise a two-dimensional array
- If (data [0] = 0 ){
- Cout <"memory allocation failed! Program termination. "<Endl;
- Exit (1 );
- }
- For (int j = 0; j <x; ++ j)
- For (int k = 0; k <y; ++ k)
- Data [j] [k] = new double [z]; // creates all one-dimensional arrays.
- If (data [0] [0] = 0 ){
- Cout <"memory allocation failed! Program termination. "<Endl;
- Exit (1 );
- }
- For (int I = 0; I <x; ++ I)
- For (int j = 0; j <y; ++ j)
- For (int k = 0; k <z; ++ k)
- Data [j] [k] = I * y * z + j * z + k;
- Display (data );
- De_lete (data );
- Getch ();
- }
- Void display (double *** data) // display each element
- {
- Cout <"The following arrays meet the requirements:" <endl;
- For (int I = 0; I <x; ++ I ){
- For (int j = 0; j <y; ++ j ){
- For (int k = 0; k <z; ++ k)
- Cout <data [j] [k] <"\ t ";
- Cout <endl;
- }
- Cout <endl;
- }
- }
- Void de_lete (double *** data) // release the dynamically allocated memory space
- {
- For (int I = 0; I <x; ++ I)
- For (int j = 0; j <y; ++ j)
- Delete [] data [j];
- For (int I = 0; I <x; ++ I)
- Delete [] data;
- Delete [] data;
- }
The above is our introduction to the C ++ Dynamic Array Operations.