轉(zhuǎn)至:http://www.cnblogs.com/lxshanye/archive/2013/05/20/3088558.htmlC++動(dòng)態(tài)數(shù)組1.變長一維數(shù)組 1: //文件名: array01.cpp 2: #include<iostream> 3: using namespace std; 4: int main() 5: { 6: int len; 7: cin>>len; 8: //用指針p指向new動(dòng)態(tài)分配的長度為len*sizeof(int)的內(nèi)存空間 9: int *p=new int[len]; 10: ........... 11: delete[] p; 12: return 0; 13: } 注意int *p=new int[len];這一句,你不能這樣做: 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);//聲明變長數(shù)組 10: for(int i=0;i<len;i++) 11: { 12: array[i]=i; 13: cout<<array[i]<<"/t"; 14: } 15: return 0; 16: } 這里的變長數(shù)組讓我聯(lián)想到了java的java.util包中的vector和C#中的ArrayList,,它們也可以在各自的語言中實(shí)現(xiàn)變長數(shù)組。不過C++中的vector不能像C#一樣有托管的垃圾回收機(jī)制回收被占用的內(nèi)存空間,但是你可以在使用完vector后調(diào)用~vector()析構(gòu)函數(shù)釋放內(nèi)存,。 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("請輸入你所求解的線性方程組的行數(shù)x:x="); 9: scanf("%d",&x); 10: printf("請輸入你所求解的線性方程組的列數(shù)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: /*讀入數(shù)據(jù)*/ 19: printf("請按行的順序依次輸入系數(shù)的值(共%d項(xiàng)):",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("請按列的順序依次輸入常數(shù)的值(共%d項(xiàng)):",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++怎樣實(shí)現(xiàn)呢?在C++中可以通過new和delete運(yùn)算符動(dòng)態(tài)開辟和釋放空間,,其中new與C中malloc函數(shù)的功能相似,,delete與C中free函數(shù)的功能相似。用C++實(shí)現(xiàn)變長二維數(shù)組時(shí)可以采用兩種方法:雙指針方法和使用STL中vector(向量)的方法,。 1: //文件名: array04.cpp 2: #include <iostream> 3: #include <iomanip> 4: using namespace std; 5: int main() 6: { 7: int num1,//行數(shù) 8: num2;//列數(shù) 9: cout<<"Please enter the number for row and column: "<<endl; 10: cin >> num1 >> num2; 11: //為二維數(shù)組開辟空間 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: //釋放二維數(shù)組占用的空間 25: for(int m=0;m<num1;m++) 26: delete[] p[m]; 27: delete[] p; 28: return 0; 29: } 以下是運(yùn)行結(jié)果: 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, //行數(shù) 11: n; //列數(shù) 12: cout << "input value for m,n:"; 13: cin>>m>>n; 14: //注意下面這一行:vector<int后兩個(gè)">"之間要有空格!否則會被認(rèn)為是重載">>",。 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: } 以下是運(yùn)行結(jié)果: 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, //一維坐標(biāo) 12: n, //二維坐標(biāo) 13: l; //三維坐標(biāo) 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: } 運(yùn)行結(jié)果: |
|