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:
- A (*ga)[n] = new A[m][n];
- ...
- 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:
- A** ga = new A*[m];
- for(int i = 0; i < m; i++)
- ga[i] = new A[n];
- ...
- for(int i = 0; i < m; i++)
- delete []ga[i];
- 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:
- A* ga = new A[m*n];
- ...
- delete []ga;
Disadvantage: The call is not intuitive enough.
Advantage: continuous storage, n can be unknown
C ++ two-dimensional array new Application Method 4:
- Vector> ga;
- Ga. resize (m); // these three rows can be used
- For (int I = 1; I <n; I ++ )//
- Ga [I]. resize (n );//
- ...
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:
- vector ga;
- ga.resize(m*n);
Combination of method 3 and 4
C ++ two-dimensional array new Application Method 6:
Release Version 2
- A** ga = new A*[m];
- ga[0] = new A[m*n];
- for(int i = 1; i < m; i++)
- ga[i] = ga[i-1]+n;
Advantage: continuous storage, n can be unknown, and the structure is convenient. We only need to delete [] ga;