# C++ - new delete 高维数组小结

 1 #include <iostream>
2
3  using namespace std;
4
5 int main(void)
6 {
7         int **p;
8
9         p = new int*[3];
10
11         for (int i = 0; i < 3; i++)
12                 p[i] = new int[4];
13
14         for (int i = 0; i < 3; i++) {
15                 for (int j = 0; j < 4; j++)
16                         p[i][j] = i+j;
17         }
18
19         for (int i = 0; i < 3; i++)
20                 delete []p[i];
21
22         delete []p;
23
24         return 0;
25 }

然后是各种new:

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

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;

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

4. vector > ga;
ga.resize(m); //这三行可用可不用
for(int i = 1; i < n; i++) //
ga[i].resize(n); //
...

5. vector
ga;
ga.resize(m*n);

6. 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;

1.怎么进行多维数组的声明和初始化?

2.是否能够动态分配多维数组,怎么样正确析构?

3.怎么理解这些操作?

posted @ 2014-12-25 11:26  银槲  阅读(...)  评论(...编辑  收藏