?
#include
#define
?N?4
typedef?
int
?p[N];
//
方法一用到
using
?
namespace
?std;
int
?main()
{
????
int
?n?
=
?
0
;
????
//
方法一:使用typedef定義一個具有N個元素的數組類型
????p?
*
ptr1;??????
//
定義二維數組??用法與二維數組相同
????ptr1?
=
?
new
?p[N];
????
for
(
int
?i?
=
?
0
;?i?
<
?N;?i
++
)
????????
for
(
int
?j?
=
?
0
;?j?
<
?N;?j
++
)
????????????ptr1[i][j]?
=
?
++
n;
????cout?
<<
?
"
方法一:
"
?
<<
?endl;
????
for
(i?
=
?
0
;?i?
<
?N;?i
++
)
????{
????????
for
(
int
?j
=
0
;j?
<
?N;?j
++
)
????????????cout?
<<
?ptr1[i][j]?
<<
?
"
?
"
;
????????cout?
<<
?endl;
????}
????delete[]?ptr1;
????cout?
<<
?endl;
????
//
?方法二:使用數組指針
????
int
?row?
=
?N;?????
//
二維數組的行數?
????
int
?column?
=
?N;??
//
二維數組的列數
?????
????
//
分配一個指針數組,其首地址保存在pMatrix中
????
int
?
**
pMatrix?
=
?
new
?
int
*
[row];
????
//
為指針數組的每個元素分配一個數組
????
for
?(
int
?i?
=
?
0
;?i?
<
?row;?i
++
)
????????pMatrix[i]?
=
?
new
?
int
[column];
????
//
以上是分配,以下是釋放
????
for
?(
int
?i?
=
?
0
;?i?
<
?row;?i
++
)
????????delete?[column]?pMatrix[i];
????delete?[row]?pMatrix;
????
//
這些技術可用于構造一個矩陣類
????
return
?
0
;
}
from:
http://www.zahui.com/html/9/20062.htm