1:1-D Array initialization
Standard way 1:int value[100]; Value[i] has a variable value because it is not initialized;
Standard way 2:int value[100] = {--); VALUE[0],VALUE[1],VALUE[2] value is 0, without a defined initialization.
Pointer mode: int * value = new Int[n]; Not initialized
Delete array space: delete []value;
Two: two-dimensional array initialization:
Standard way 1:int value[100][100]; Value[i] has a variable value because it is not initialized;
Standard way 2:int value[100][100] = {{1,1},{2,2},{3,3}}; value[0][0,1],value[1][0,1],value[2][0,1] initialization, no definition initialized to 0
Three: Establishing a two-dimensional dynamic array array
The dynamic array mentioned here refers to an array that cannot determine the length of the array at compile time, and the program needs to allocate the memory space dynamically at run time.
1. Using pointers, assign an array of pointers, save their first address in Num, and then assign an array to each element of the pointer array
1 intj =3;//defining the number of second-dimensional arrays2 int**num =New int*[longs];//assigns an array of pointers, storing their first addresses in Num;3 for(inti =0; I < longs; i++) {//assigns an array to each element of the pointer array;4Num[i] =New Long[j];5 } 6 //The release of a dynamic two-dimensional array defined in this way requires that the array of pointers to each element of the pointer array be disposed before the array of pointers is disposed;7 for(inti =0; I < longs; i++){8 Delete[j]num[i];9num[i]=NULL;Ten } One Delete[Longs]num; ANum=null;
2. Using vectors
1 intMain () {2 intRow,column; 3Cin>>row>>column; 5 //Application Space6vector<vector<int> > A (row,vector<int>(column)); 7 //Use space8 for(intj =0; J < Row; J + +) {9 for(intK =0; k < column; k++) {TenA[J][K] = rand ()% -; One } A } - for(intj =0; J < row;j++) { -cout<<Endl; the for(intK =0;k< column;k++) { -A[J][K] = rand ()% -; -cout<<a[j][k]<<" "; - } + } - return 0; +}
C + + dynamic arrays