一直都沒好好的使用過動態二維數組,從前我一直覺得二維數組能做的,一維數組都可以。。
下面給出幾種情況,都和動態的二維數組相關的:
一、當使用vector的時候
不確定行列數的時候可以這麼干:
[cpp]
// ConsoleApplication2.cpp : 定義控制台應用程序的入口點。
//
#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;
}
// ConsoleApplication2.cpp : 定義控制台應用程序的入口點。
//
#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;
}
二、在不使用vector的時候
分三種情況
1:列先確定,行後確定
[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:行先確定,列後確定,這是一種很常見的情況
[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:行列均不固定的情況
[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;
// 為array的分配ROW行個int*數據
array = new int*[ROW]();
for (int i = 0; i < ROW; i++)
{
// 為array[i]的每一列分配COL個int的空間
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;
// 為array的分配ROW行個int*數據
array = new int*[ROW]();
for (int i = 0; i < ROW; i++)
{
// 為array[i]的每一列分配COL個int的空間
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;
}
最後的總結:其實大多數情況下,二維數組都可以轉換成一維數組來使用