Discussion on the Application of C ++ Dynamic Array

Source: Internet
Author: User

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:

 
 
  1. # Include <iostream>
  2. # Include <conio. h>
  3. # Include <cstdlib>
    // This header file contains the function exit (). Because C ++ can recognize exit (), this header file can be omitted.
  4. Using namespace std;
  5. Void display (double ***);
  6. Void de_lete (double ***);
  7. Int x, y, z; // global variable
  8. Void main ()
  9. {
  10. Cout <"Enter the values of x, y, and z in the three-dimensional array data [X] [Y] [Z] respectively:" <endl;
  11. While (cin> x> y> z & (x <0 | y <0 | z <0 ))
  12. Cout <"the input is invalid. Please enter it again! "<Endl;
  13. Double *** data;
  14. Data = new double ** [x]; // creates a second-level pointer array representing the names of two-dimensional arrays that comprise a three-dimensional array
  15. If (data = 0 ){
  16. Cout <"memory allocation failed! Program termination. "<Endl;
  17. Exit (1 );
  18. }
  19. For (int j = 0; j <x; ++ j)
  20. Data [j] = new double * [y]; // create a pointer array representing the names of all one-dimensional arrays that comprise a two-dimensional array
  21. If (data [0] = 0 ){
  22. Cout <"memory allocation failed! Program termination. "<Endl;
  23. Exit (1 );
  24. }
  25. For (int j = 0; j <x; ++ j)
  26. For (int k = 0; k <y; ++ k)
  27. Data [j] [k] = new double [z]; // creates all one-dimensional arrays.
  28. If (data [0] [0] = 0 ){
  29. Cout <"memory allocation failed! Program termination. "<Endl;
  30. Exit (1 );
  31. }
  32. For (int I = 0; I <x; ++ I)
  33. For (int j = 0; j <y; ++ j)
  34. For (int k = 0; k <z; ++ k)
  35. Data [j] [k] = I * y * z + j * z + k;
  36. Display (data );
  37. De_lete (data );
  38. Getch ();
  39. }
 
 
  1. Void display (double *** data) // display each element
  2. {
  3. Cout <"The following arrays meet the requirements:" <endl;
  4. For (int I = 0; I <x; ++ I ){
  5. For (int j = 0; j <y; ++ j ){
  6. For (int k = 0; k <z; ++ k)
  7. Cout <data [j] [k] <"\ t ";
  8. Cout <endl;
  9. }
  10. Cout <endl;
  11. }
  12. }
 
 
  1. Void de_lete (double *** data) // release the dynamically allocated memory space
  2. {
  3. For (int I = 0; I <x; ++ I)
  4. For (int j = 0; j <y; ++ j)
  5. Delete [] data [j];
  6. For (int I = 0; I <x; ++ I)
  7. Delete [] data;
  8. Delete [] data;
  9. }

The above is our introduction to the C ++ Dynamic Array Operations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.