這裏說的變長數組是指在編譯時不能肯定數組長度,程序在運行時須要動態分配內存空間的數組。實現變長數組最簡單的是變長一維數組,你能夠這樣作: java
1: //文件名: array01.cpp ios
2: #include<iostream> 數組
3: using namespace std; 安全
4: int main() 函數
5: { 測試
6: int len; spa
7: cin>>len; 設計
8: //用指針p指向new動態分配的長度爲len*sizeof(int)的內存空間 指針
9: int *p=new int[len]; ip
10: ...........
11: delete[] p;
12: return 0;
13: }
注意int *p=new int[len];這一句,你不能這樣作:
int p[len];
C++編譯器會報錯說len的大小不能肯定,由於用這種形式聲明數組,數組的大小須要在編譯時肯定。並且這樣也不行:
int p[]=new int[len];
編譯器會說不能把int*型轉化爲int[]型,由於用new開闢了一段內存空間後會返回這段內存的首地址,因此要把這個地址賦給一個指針,因此要用int *p=new int[len];
array01.cpp實現了一個變長的一維數組,可是要養成一個好習慣,就是注意要註銷指針p,使程序釋放用new開闢的內存空間。
固然使用C++標準模版庫(STL)中的vector(向量)也能夠實現變長數組:
1: //文件名: array02.cpp
2: #include<iostream>
3: #include<vector>
4: using namespace std;
5: int main()
6: {
7: int len;
8: cin>>len;
9: vector<int> array(len);//聲明變長數組
10: for(int i=0;i<len;i++)
11: {
12: array[i]=i;
13: cout<<array[i]<<"/t";
14: }
15: return 0;
16: }
這裏的變長數組讓我聯想到了java的java.util包中的vector和C#中的ArrayList,它們也能夠在各自的語言中實現變長數組。不過 C++中的vector不能像C#同樣有託管的垃圾回收機制回收被佔用的內存空間,可是你能夠在使用完vector後調用~vector()析構函數釋放 內存。
2.變長n維數組
變長的n維數組實現起來有些麻煩,可是在工程與軟件設計應用中常使用的是二維數組,因此在這裏着重介紹變長的二維數組,變長的n維數組能夠按照相似的方法實現。首先看一個經典的用C實現變長二維數組的例子:
1: //文件名: array03.c
2: #include <stdio.h>
3: #include <malloc.h>
4: void main()
5: {
6: int x,y,i,j;
7: float **a,*b;
8: printf("請輸入你所求解的線性方程組的行數x:x=");
9: scanf("%d",&x);
10: printf("請輸入你所求解的線性方程組的列數y:y=");
11: scanf("%d",&y);
12: a=(float **)malloc(sizeof(float *) *x);
13: b=(float *)malloc(sizeof(float) *x);
14: for(i=0;i<x;i++)
15: {
16: *(a+i)=(float *)malloc(sizeof(float) *y);
17: }
18: /*讀入數據*/
19: printf("請按行的順序依次輸入係數的值(共%d項):",x*y);
20: for(i=0;i<=x-1;i++)
21: for(j=0;j<=y-1;j++)
22: scanf("%f",&a[i][j]);
23: printf("請按列的順序依次輸入常數的值(共%d項):",x);
24: for(j=0;j<=x-1;j++)
25: scanf("%f",&b[j]);
26: printf("您輸入方程組的增廣矩陣爲:/n");
27: for(i=0;i<=x-1;i++)
28: {
29: for(j=0;j<=y-1;j++)
30: printf("%.5f ",a[i][j]);
31: printf("%.5f ",b[i]);
32: printf("/n");
33: }
34: free(b);
35: for(i=0;i<x;i++)
36: free (*(a+i));
37: }
那麼用C++怎樣實現呢?在C++中能夠經過new和delete運算符動態開闢和釋放空間,其中new與C中malloc函數的功能類似,delete 與C中free函數的功能類似。用C++實現變長二維數組時能夠採用兩種方法:雙指針方法和使用STL中vector(向量)的方法。
首先介紹一下雙指針方法,在這裏雙指針就是指像指針的指針,好比你能夠這樣聲明一個數組:
int **p = new int*[num1];
而對每個*p(一共num1個*p)申請一組內存空間:
for(int i=0; i<num1; ++i)
p[i] = new int[num2];
其中,num1是行數,num2是數組的列數。測試的源程序以下:
1: //文件名: array04.cpp
2: #include <iostream>
3: #include <iomanip>
4: using namespace std;
5: int main()
6: {
7: int num1,//行數
8: num2;//列數
9: cout<<"Please enter the number for row and column: "<<endl;
10: cin >> num1 >> num2;
11: //爲二維數組開闢空間
12: int **p = new int*[num1];
13: for(int i=0; i<num1; ++i)
14: p[i] = new int[num2];
15: for(int j=0;j<num1;j++)
16: {
17: for(int k=0;k<num2;k++)
18: {
19: p[j][k]=(j+1)*(k+1);
20: cout<<setw(6)<<p[j][k]<<':'<<setw(8)<<&p[j][k];
21: }
22: cout<<endl;
23: }
24: //釋放二維數組佔用的空間
25: for(int m=0;m<num1;m++)
26: delete[] p[m];
27: delete[] p;
28: return 0;
29: }
如下是運行結果:
Please enter the number for row and column:
4 5
1:004915F0 2:004915F4 3:004915F8 4:004915FC 5:00491600
2:00491180 4:00491184 6:00491188 8:0049118C 10:00491190
3:00491140 6:00491144 9:00491148 12:0049114C 15:00491150
4:00491100 8:00491104 12:00491108 16:0049110C 20:00491110
Press any key to continue
程序清單array04.cpp能夠顯示分配的內存空間單元的地址,你們能夠看到,因爲數組空間是動態分配的,數組行之間的地址空間是不連續的,由於不一樣行的數組元素的地址空間是用不一樣的new來分配的。而每一行之中列之間的地址空間是連續的。
那麼用vector(向量)怎樣實現二維數組呢?如下給出源程序:
1: //文件名: array05.cpp
2: #include <iostream>
3: #include <vector>
4: #include <iomanip>
5: using namespace std;
6: int main()
7: {
8: int i,
9: j,
10: m, //行數
11: n; //列數
12: cout << "input value for m,n:";
13: cin>>m>>n;
14: //注意下面這一行:vector<int後兩個">"之間要有空格!不然會被認爲是重載">>"。
15: vector<vector<int> > vecInt(m, vector<int>(n));
16: for (i = 0; i < m; i++)
17: for (j = 0; j < n; j++)
18: vecInt[i][j] = i*j;
19: for (i = 0; i < m; i++)
20: {
21: for (j = 0; j < n; j++)
22: cout<<setw(5)<<vecInt[i][j]<<":"<<setw(9)<<&vecInt[i][j];
23: cout<<endl;
24: }
25: return 0;
26: }
如下是運行結果:
input value for m,n:3 4
0: 00491180 0: 00491184 0: 00491188 0: 0049118C
0: 00491140 1: 00491144 2: 00491148 3: 0049114C
0: 00491100 2: 00491104 4: 00491108 6: 0049110C
Press any key to continue
你們能夠看到,這裏vector中元素的內存的地址分配也有同雙指針實現的二維數組有一樣的特色。不過用vector的方法比使用雙指針簡單地多,分配內 存空間時會更安全,數組初始化代碼也更簡單,因此本人建議使用STL中的vector來實現變長多維數組。如下是一個變長三維數組:)
1: //文件名: array06.cpp
2: #include <iostream>
3: #include <vector>
4: #include <iomanip>
5: using namespace std;
6: int main()
7: {
8: int i,
9: j,
10: k,
11: m, //一維座標
12: n, //二維座標
13: l; //三維座標
14: cout << "input value for m,n,l:";
15: cin>>m>>n>>l;
16: vector<vector<vector<int> > > vecInt(m, vector<vector<int> >(n, vector<int>(l)));
17: for (i = 0; i < m; i++)
18: for (j = 0; j < n; j++)
19: for(k = 0; k < l; k++)
20: vecInt[i][j][k] = i+j+k;
21: for (i = 0; i < m; i++)
22: {
23: for (j = 0; j < n; j++)
24: {
25: for(k = 0; k<l; k++)
26: cout<<setw(5)<<vecInt[i][j][k]<<":"<<setw(9)<<&vecInt[i][j][k];
27: cout<<endl;
28: }
29: cout<<endl;
30: }
31: return 0;
32: }
運行結果: input value for m,n,l:2 3 4 0: 00492FE0 1: 00492FE4 2: 00492FE8 3: 00492FEC 1: 00492FA0 2: 00492FA4 3: 00492FA8 4: 00492FAC 2: 00492F60 3: 00492F64 4: 00492F68 5: 00492F6C 1: 00492EC0 2: 00492EC4 3: 00492EC8 4: 00492ECC 2: 00492E80 3: 00492E84 4: 00492E88 5: 00492E8C 3: 00492E40 4: 00492E44 5: 00492E48 6: 00492E4C