C ++ new Delete dynamically applies for two-dimensional arrays-microtemperature lunch-blog Garden
C ++ new Delete dynamically applies for two-dimensional arrays
When I was learning C ++, I didn't talk about how to use new Delete to apply for a dynamic two-dimensional array space. I couldn't find a simple one on the Internet.CodeAnd write it as follows:
Take a two-dimensional int array as an example --
Cin> m> N; // M, N indicates the number of rows and columns respectively.
Int ** P;
P = new int * [m];
For (INT I = 0; I <m; I ++)
P [I] = new int [N];
Then we can assign a value to P [I] [J] as a two-dimensional array.
Code explanation:
Point a pointer to a pointer array, and apply a one-dimensional space for each pointer in the pointer array.
LastDon't forget to release:
For (INT I = 0; I <m; I ++)
Delete [] P [I];
Delete [] P;
Introduction complete!
When I was learning C ++, I didn't talk about how to use new Delete to apply for a dynamic two-dimensional array space. I couldn't find the concise code on the Internet, so I wrote the following myself:
Take a two-dimensional int array as an example --
Cin> m> N; // M, N indicates the number of rows and columns respectively.
Int ** P;
P = new int * [m];
For (INT I = 0; I <m; I ++)
P [I] = new int [N];
Then we can assign a value to P [I] [J] as a two-dimensional array.
Code explanation:
Point a pointer to a pointer array, and apply a one-dimensional space for each pointer in the pointer array.
LastDon't forget to release:
For (INT I = 0; I <m; I ++)
Delete [] P [I];
Delete [] P;
Introduction complete!