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.