C ++ two-dimensional array initialization related application skills

Source: Internet
Author: User

In C ++ programming language, array operations are a very basic and important application technology. In this article, we will introduce the operations related to C ++ two-dimensional array initialization, so that you can master the application technology in this area. The two-dimensional array of C ++ cannot be initialized using variables. The following code is certainly not compiled:

 
 
  1. int i=5;  
  2. int j=4;  
  3. int a[i][j]; 

Code like this must be the experience of many C ++ beginners like me. If the array is determined in the compilation phase, the variable cannot be used as the dimension of the array. below, we use a template class to complete the C ++ two-dimensional array initialization function.

 
 
  1. Template <class T>
  2. Class Array2D {
  3. Private:
  4. T * pData;
  5. Int dim1;
  6. Int dim2;
  7. Int dim1Index;
  8. Class Array1D {
  9. Private:
  10. Int length;
  11. T * start;
  12. Public:
  13. Array1D (T * start, int length): length (length), start (start ){}
  14. T & operator [] (int index ){
  15. If (index> length ){
  16. Throw out_of_range ("the second dimension of the array is out of bounds ");
  17. } Else {
  18. Return * (start + index );
  19. }
  20. }
  21. };
  22. Public:
  23. Array2D (int dim1, int dim2 ){
  24. This-> dim1dim1 = dim1;
  25. This-> dim2dim2 = dim2;
  26. Int size = dim1 * dim2;
  27. PData = new T [size];
  28. }
  29. Array1D operator [] (int index ){
  30. Return Array1D (pData + index * dim1, dim2 );
  31. }
  32. Void print (){
  33. For (int I = 0; I <dim1; I ++ ){
  34. For (int j = 0; j <dim2; j ++ ){
  35. Cout <* (pData + dim1 * I + j) <"";
  36. }
  37. Cout <endl;
  38. }
  39. }
  40. };
  41. Int main (){
  42. Int index1 = 2;
  43. Int index2 = 2;
  44. Array2D <int> test (index1, index2 );
  45. Test [0] [0] = 1;
  46. Test [0] [1] = 2;
  47. Test [1] [0] = 3;
  48. Test [1] [1] = 4;
  49. Test. print ();
  50. }

Using a template class to implement this function is a good choice in C ++ two-dimensional array initialization. However, in practice, it is rarely written in this way, this is the method provided in more than tive C ++ to demonstrate the proxy mode. Array1D exists as a proxy class.

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.