Multidimensional Dynamic Array

Source: Internet
Author: User

In C ++ Primer, there is no multi-dimensional array in C ++ and only an array of element division arrays.
For example, to create a two-dimensional integer array, you must first create a one-dimensional dynamic array, which consists of int * type pointers. Int * is the type of this one-dimensional int pointer array.
 
The following is an example
[Cpp]
// String. cpp: Defines the entry point for the console application.
//
 
# Include "stdafx. h"
# Include <iostream>
 
Typedef int * IntArrayPtr; // (1)
 
Void fill_array (int a [], int size );
Void sort (int a [], int size );
 
Int main (int argc, char * argv [])
{
Using namespace std;
Int row, col;
Cout <"Enter the row and column dimensions of the array: \ n ";
Cin> row> col;
 
IntArrayPtr * m; // (2)
Int I, j;
M = new IntArrayPtr [row];
 
// Apply for memory
For (I = 0; I <row; I ++)
M [I] = new int [col]; // m is now an array of row * col
 
Cout <"Enter" <row <"rows"
<Col <"integers each: \ n ";

/// Assign a value
For (I = 0; I <row; I ++)
For (j = 0; j <col; j ++)
Cin> m [I] [j];
 
/// Print a two-dimensional Dynamic Array
Cout <"Echoing the 2 dimetricarray: \ n ";
For (I = 0; I <row; I ++)
{
For (j = 0; j <col; j ++)
Cout <m [I] [j] <"";
Cout <endl;
}
 
/// Release the memory
For (I = 0; I <row; I ++)
Delete [] m [I];
Delete [] m;
Return 0;
}


 
 


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.