Turn from:
There is a two-dimensional array called New in the C + + programming language, which is more flexible to use, and there are several ways to help us implement certain functions. Here we will summarize several C + + two-dimensional array new application methods, to conduct a review.
C + + two-dimensional array new application method one:
- A (*GA) [n] = new A[m][n];
- ...
- delete []ga;
Disadvantage: n must be a known
Advantages: Call intuitive, continuous storage, program concise (after testing, destructors can be called correctly)
C + + two-dimensional array new application method two:
- 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;
Cons: Non-sequential storage, cumbersome procedures, GA for a** type
Pros: Call intuitive, n may not be known
C + + two-dimensional array new application method three:
- * GA = new a[m*n];
- ...
- delete []ga;
Cons: Call not intuitive
Advantage: Continuous storage, n may not be known
C + + two-dimensional array new application Mode four:
- Vector > ga;
- Ga.resize (m); These three lines can be used
- for (int i = 1; I < n; i++)//
- Ga[i].resize (n); //
- ...
Disadvantages: Non-continuous storage, debugging is not convenient, the compiler speed down, program expansion (actual speed difference is not small)
Advantages: Call intuitive, automatic destruction and free memory, you can call the STL-related functions, dynamic growth
C + + two-dimensional array new application Mode five:
- Vector ga;
- Ga.resize (M*n);
The combination of method 3,4
C + + two-dimensional array new application Method VI:
Improved version of 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;
C + + Application two-dimensional array