Comments on several application methods of C ++ two-dimensional array new

Source: Internet
Author: User

In C ++ programming language, there is a two-dimensional array called new. It is used in a flexible way and can be used in many ways to help us implement some specific functions. Here, we will summarize several application methods of the C ++ two-dimensional array new to review them one by one.

C ++ two-dimensional array new Application Method 1:

 
 
  1. A (*ga)[n] = new A[m][n];   
  2. ...   
  3. delete []ga;  

Disadvantage: n must be known

Advantages: intuitive calling, continuous storage, and simple program (tested, the Destructor can be correctly called)

C ++ two-dimensional array new Application Method 2:

 
 
  1. A** ga = new A*[m];   
  2. for(int i = 0; i < m; i++)   
  3. ga[i] = new A[n];   
  4. ...   
  5. for(int i = 0; i < m; i++)   
  6. delete []ga[i];   
  7. delete []ga;  

Disadvantages: non-continuous storage, cumbersome procedures, ga is A ** type

Advantage: The call is intuitive. n can be unknown.

C ++ two-dimensional array new Application Method 3:

 
 
  1. A* ga = new A[m*n];   
  2. ...   
  3. delete []ga;  

Disadvantage: The call is not intuitive enough.

Advantage: continuous storage, n can be unknown

C ++ two-dimensional array new Application Method 4:

 
 
  1. Vector> ga;
  2. Ga. resize (m); // these three rows can be used
  3. For (int I = 1; I <n; I ++ )//
  4. Ga [I]. resize (n );//
  5. ...

Disadvantages: non-continuous storage, inconvenient debugging, lower Compilation speed, and program expansion (the actual speed is not much different)

Advantage: intuitive call, automatic analysis and memory release, can call stl related functions, dynamic growth

C ++ two-dimensional array new Application Method 5:

 
 
  1. vector ga;   
  2. ga.resize(m*n);  

Combination of method 3 and 4

C ++ two-dimensional array new Application Method 6:

Release Version 2

 
 
  1. A** ga = new A*[m];   
  2. ga[0] = new A[m*n];   
  3. for(int i = 1; i < m; i++)   
  4. ga[i] = ga[i-1]+n;  

Advantage: continuous storage, n can be unknown, and the structure is convenient. We only need to delete [] ga;

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.