The use of dynamic two-dimensional arrays is discussed here

Source: Internet
Author: User

I have never used a dynamic two-dimensional array. I used to think that a two-dimensional array can be used ..


The following situations are related to dynamic two-dimensional arrays:

 

 

1. When using vector

When you are not sure about the number of columns, you can do this:

[Cpp]
// Leleapplication2.cpp: defines the entry point of the console application.
//
 
# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"
 
# Define COL 4
 
Int * Array (int radix)
{
Int * array = new int [4];
For (int I = 0; I <COL; I ++)
{
Array [I] = (I + 1) * radix;
}
 
Return array;
}
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Vector <int *> array;
Array. push_back (Array (2 ));
Array. push_back (Array (3 ));
 
For (vector <int * >:: iterator it = array. begin (); it! = Array. end (); * it ++)
{
Int * test = * it;
For (int I = 0; I <COL; I ++)
{
Cout <test [I] <"";
}
Cout <endl;
}
 
Return 0;
}

// Leleapplication2.cpp: defines the entry point of the console application.
//

# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"

# Define COL 4

Int * Array (int radix)
{
Int * array = new int [4];
For (int I = 0; I <COL; I ++)
{
Array [I] = (I + 1) * radix;
}

Return array;
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
Vector <int *> array;
Array. push_back (Array (2 ));
Array. push_back (Array (3 ));

For (vector <int * >:: iterator it = array. begin (); it! = Array. end (); * it ++)
{
Int * test = * it;
For (int I = 0; I <COL; I ++)
{
Cout <test [I] <"";
}
Cout <endl;
}

Return 0;
}

2. When vector is not used

In three cases

1: confirm the column first, and then confirm the row.
[Cpp]
# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"
 
# Define ROW 2
# Define COL 3
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int (* array) [COL];
 
Array = new int [ROW] [COL];
 
Int value = 0;
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Array [I] [j] = ++ value;
}
}
 
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Cout <array [I] [j] <"";
}
Cout <endl;
}
 
Delete [] array;
 
Return 0;
}

# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"

# Define ROW 2
# Define COL 3

Int _ tmain (int argc, _ TCHAR * argv [])
{
Int (* array) [COL];

Array = new int [ROW] [COL];

Int value = 0;
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Array [I] [j] = ++ value;
}
}

For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Cout <array [I] [j] <"";
}
Cout <endl;
}

Delete [] array;

Return 0;
}

2: The rows are determined first, and the columns are determined later. This is a common situation.
[Cpp]
# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"
 
# Define ROW 2
# Define COL 3
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int * array [ROW];
 
Int value = 0;
For (int I = 0; I <ROW; I ++)
{
Array [I] = new int [COL];
}
 
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Array [I] [j] = ++ value;
}
}
 
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Cout <array [I] [j] <"";
}
Cout <endl;
}
 
For (int I = 0; I <ROW; I ++)
{
If (array [I])
{
Delete [] array [I];
Array [I] = NULL;
}

}
 
Return 0;
}

# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"

# Define ROW 2
# Define COL 3

Int _ tmain (int argc, _ TCHAR * argv [])
{
Int * array [ROW];

Int value = 0;
For (int I = 0; I <ROW; I ++)
{
Array [I] = new int [COL];
}

For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Array [I] [j] = ++ value;
}
}

For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Cout <array [I] [j] <"";
}
Cout <endl;
}

For (int I = 0; I <ROW; I ++)
{
If (array [I])
{
Delete [] array [I];
Array [I] = NULL;
}

}

Return 0;
}

3: rows and columns are not fixed.
[Cpp]
# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"
 
# Define ROW 2
# Define COL 3
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int value = 0;
Int ** array;
 
// Allocate ROW int X Data for array
Array = new int * [ROW] ();
 
For (int I = 0; I <ROW; I ++)
{
// Allocate COL int space for each column of array [I]
Array [I] = new int [COL];
}
 
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Array [I] [j] = ++ value;
}
}
 
For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Cout <array [I] [j] <"";
}
Cout <endl;
}
 
For (int I = 0; I <ROW; I ++)
{
If (array [I])
{
Delete [] array [I];
Array [I] = NULL;
}
 
}
 
Return 0;
}

# Include "stdafx. h"
# Include "iostream"
Using namespace std;
# Include "vector"

# Define ROW 2
# Define COL 3

Int _ tmain (int argc, _ TCHAR * argv [])
{
Int value = 0;
Int ** array;

// Allocate ROW int X Data for array
Array = new int * [ROW] ();

For (int I = 0; I <ROW; I ++)
{
// Allocate COL int space for each column of array [I]
Array [I] = new int [COL];
}

For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Array [I] [j] = ++ value;
}
}

For (int I = 0; I <ROW; I ++)
{
For (int j = 0; j <COL; j ++)
{
Cout <array [I] [j] <"";
}
Cout <endl;
}

For (int I = 0; I <ROW; I ++)
{
If (array [I])
{
Delete [] array [I];
Array [I] = NULL;
}

}

Return 0;
}

Conclusion: In most cases, two-dimensional arrays can be converted into one-dimensional 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.