C + + dynamic arrays

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.