C ++ implements Dynamic Arrays

Source: Internet
Author: User

I. Double pointer Method

A double pointer is a pointer like a pointer. For example, you can declare an array and release the space of an array as follows:
 // Declare a two-dimensional dynamic array with double pointers <br/> int ** arr = new int * [X]; // understand the meaning of double pointers, pointer-> array pointer-> two-dimensional array <br/> for (INT I = 0; I <X; ++ I) <br/> arr [I] = new int [y]; // it can be understood that an array is opened on a single bit of a one-dimensional array, it also forms a two-dimensional array </P> <p> // use Delete to release space <br/> for (INT m = 0; m <X; m ++) <br/> Delete [] P [m]; <br/> Delete [] P; <br/>

Ii. STL Vector

The declaration method is as follows:

// Declare an array of M * n with a vector <br/> vector <int> vecarr (M, vector <int> (n )); // note> there must be spaces between them, otherwise it will be misjudged as> operator <br/> for (INT I = 0; I <m; I ++) <br/> for (j = 0; j <n; j ++) <br/> vecarr [I] [J] = I * J; </P> <p> // declare an I * j * K 3D array <br/> vector <int> vecant (m, vector <vector <int> (n, vector <int> (l); <br/> for (INT I = 0; I <m; I ++) <br/> for (Int J = 0; j <n; j ++) <br/> for (k = 0; k <L; k ++) <br/> vecint [I] [J] [k] = I + J + K;


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.