1. 程式人生 > >C++中如何定義動態陣列

C++中如何定義動態陣列

 

首先:為什麼需要動態定義陣列呢?
  這是因為,很多情況下,在預編譯過程階段,陣列的長度是不能預先知道的,必須在程式執行時動態的給出
  但是問題是,c++要求定義陣列時,必須明確給定陣列的大小,要不然編譯通不過 
 
  如: int Array[5];正確

       int i=5;
       int Array[i]; 錯誤 因為在編譯階段,編譯器並不知道 i 的值是多少

   那麼,我們該如何解決定義長度未知的陣列呢?
   答案是:new 動態定義陣列 

   因為new 就是用來動態開闢空間的,所以當然可以用來開闢一個數組空間
   
   這樣,下面的語句:
    int size=50;
    int *p=new int[size]; 是正確的
 
   但是二維動態陣列能不能也這樣定義呢
   
  int size=50,Column=50;
  int (*p)[Column]=new int [size][Column]

  這樣的語句,編譯器通不過,為什麼呢?
  首先 new int[size][Column] 就是動態生成時確定的,所以它沒有錯
  那麼就是 int(*p)[Column],這句有問題了,這句為什麼不對呢, 那是因為,這是一個定義語句,而定義語句先經過編譯器進行編譯,當編譯器執行到此處時,發現Column 不是常數,因此不能通過編譯。 而之所以編譯器認為Column 不是常數,是因為編譯階段,編譯器起的作用是查語法錯誤,和預分配空間,它並不執行程式,因此,沒有執行那個賦值語句(只是對這個語句檢查錯誤,和分配空間),因此編譯階段,它將認為column 是個變數。所以上面的二維陣列定義是錯誤的, 它不能通過編譯。

  改成這樣:
  int size=50
  int (*p)[50]=new int [size][50]
  便正確了。

   由此可見,這種動態分配陣列,僅對一維陣列空間是真正動態分配的。
   但是如何真正的動態分配二維陣列呢,即如果Column 也不能預先知道的話,該如何處理呢?
   
   上面的動態分配已經不能滿足我們的要求,因為上面動態分配只對一維陣列是真正動態的,對二維陣列的話,必須編譯之前預先知道二維陣列每一列的長度,而這個長度在很多情況下是不能預先知道的,所以我們得結合其他方法來解決這個問題。
   
   既然一維是真正的動態分配的話,那我們利用這一特性定義一個指標陣列。
   
   int **p= new int*[size];//定義指標陣列 
   int *p[5];//  假若知道二維陣列的行數為5

   然後對指標陣列中的每一個指標分配一個一維陣列空間,這樣便動態定義了二維陣列
  
   事實上,我認為指標陣列的主要用途,就在於動態定義多維陣列
    
    for(int i=0;i<size;i++)
   {
     p[i]=new int[Column];
   }
   
   執行完畢後,一個二維陣列便被動態的成功建立

-----------------------------------

 

例子:

 

   size =6;

 

   column =5

 

   int **p=new int*[size];

 

    for(int i=0;i<size;i++)
   {
     p[i]=new int[Column];
   }

 

 

 

所生成的動態陣列如下圖所示:

 

 

 
   
 

  最後 ,因為呼叫了new, 千萬千萬別忘記在用完之後,將其所佔資源 delete 掉

 

  下面是delete方法:

    for(int i=0;i<size;i++)
   {

           delete []  p[i];   // 要在指標前加[] , 否則的話 只釋放p[i]所指的第一個單元所佔的空間
   }

 

   delete [] p;     //最後不要忘掉 釋放掉開闢的指標陣列  :》

 

// ArrayTest.cpp : 定義控制檯應用程式的入口點。
//
 
 
#include "stdafx.h"
#include "ArrayTest.h"
#include <afxstr.h>
 
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
 
// 唯一的應用程式物件
 
 
CWinApp theApp;
 
 
using namespace std;
 
 
// ArrayTest.cpp : 定義控制檯應用程式的入口點。
//
 
 
CString** CreateCStringArray(UINT colcount, UINT rowcount){
	CString** strMsg = new CString*[colcount];
	for (UINT i = 0; i < colcount; i++)
	{
		strMsg[i] = new CString[rowcount];
	}
	return strMsg;
}
POINT** CreatePOINTArray(UINT colcount, UINT rowcount)
{
	POINT** Point = new POINT*[colcount];
	for (UINT i = 0; i < colcount; i++)
	{
		Point[i] = new POINT[rowcount];
	}
	return Point;
}
 
 
void DeleteCStringArray(UINT size, CString** pstr)
{
	for (unsigned int i = 0; i < size; i++)
	{
		delete[]  pstr[i];   // 要在指標前加[] , 否則的話 只釋放p[i]所指的第一個單元所佔的空間
	}
}
 
 
void DeletePOintArray(UINT size, POINT** pPoint)
{
	for (UINT i = 0; i < size; i++)
	{
		delete[]  pPoint[i];   // 要在指標前加[] , 否則的話 只釋放p[i]所指的第一個單元所佔的空間
	}
}
 
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
 
 
	HMODULE hModule = ::GetModuleHandle(NULL);
 
	if (hModule != NULL)
	{
		// 初始化 MFC 並在失敗時顯示錯誤
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO:  更改錯誤程式碼以符合您的需要
			_tprintf(_T("錯誤:  MFC 初始化失敗\n"));
			nRetCode = 1;
		}
		else
		{
			// TODO:  在此處為應用程式的行為編寫程式碼。
 
 
			// 定義字串陣列--動態陣列
			CString** strMsg = CreateCStringArray(8, 16);
			strMsg[0][0] = L"AddressWidth";
			strMsg[0][1] = L"Architecture";
			strMsg[0][2] = L"Availability";
			strMsg[0][3] = L"Caption";
			strMsg[0][4] = L"ConfigManagerErrorCode";
			strMsg[0][5] = L"ConfigManagerUserConfig";
			strMsg[0][6] = L"CpuStatus";
			strMsg[0][7] = L"CreationClassName";
			strMsg[0][8] = L"CurrentClockSpeed";
 
 
			strMsg[2][0] = L"InstallDate";
			strMsg[2][1] = L"L2CacheSize";
			strMsg[2][2] = L"L2CacheSpeed";
			strMsg[2][3] = L"L3CacheSize";
			strMsg[2][4] = L"L3CacheSpeed";
			strMsg[2][5] = L"LastErrorCod";
			strMsg[2][6] = L"Level";
			strMsg[2][7] = L"LoadPercentage";
			strMsg[2][8] = L"Manufacturer";
			strMsg[2][9] = L"MaxClockSpeed";
			strMsg[2][10] = L"Name";
			strMsg[2][11] = L"NumberOfCores";
			strMsg[2][12] = L"NumberOfLogicalProcessors";
			strMsg[2][13] = L"OtherFamilyDescription";
			strMsg[2][14] = L"PNPDeviceID";
			strMsg[2][15] = L"PowerManagementCapabilities";
 
 
			strMsg[4][0] = L"ProcessorId";
			strMsg[4][1] = L"ProcessorType";
			strMsg[4][2] = L"Revision";
			strMsg[4][3] = L"Role";
			strMsg[4][4] = L"SocketDesignation";
			strMsg[4][5] = L"Status";
			strMsg[4][6] = L"StatusInfo";
			strMsg[4][7] = L"Stepping";
			strMsg[4][8] = L"SystemCreationClassName";
			strMsg[4][9] = L"SystemName";
			strMsg[4][10] = L"UniqueId";
			strMsg[4][11] = L"UpgradeMethod";
			strMsg[4][12] = L"VoltageCaps";
			////////////////////////////////////////////////////////////////////////////
			strMsg[4][13] = L"ErrorCleared";
			strMsg[4][14] = L"ErrorDescription";
			strMsg[4][15] = L"ExtClock";
 
 
			strMsg[6][0] = L"InstallDate";
			strMsg[6][1] = L"L2CacheSize";
			strMsg[6][2] = L"L2CacheSpeed";
			strMsg[6][3] = L"L3CacheSize";
			strMsg[6][4] = L"L3CacheSpeed";
			strMsg[6][5] = L"LastErrorCod";
			strMsg[6][6] = L"Level";
			strMsg[6][7] = L"LoadPercentage";
			strMsg[6][8] = L"Manufacturer";
			strMsg[6][9] = L"MaxClockSpeed";
			strMsg[6][10] = L"Name";
			strMsg[6][11] = L"NumberOfCores";
			strMsg[6][12] = L"NumberOfLogicalProcessors";
			strMsg[6][13] = L"OtherFamilyDescription";
			strMsg[6][14] = L"PNPDeviceID";
			strMsg[6][15] = L"PowerManagementCapabilities";
 
 
			for (UINT i = 0; i < 8; i++)
			{
				for (UINT j = 0; j < 16; j++)
				{
					printf("%ls\n", strMsg[i][j]);
				}
				printf("--------------i = %d\n", i);
			}
 
 
			DeleteCStringArray(8, strMsg);
			//// 定義座標陣列
			POINT** Point = CreatePOINTArray(8, 16);
			POINT p1;
			p1.x = 1.0;
			p1.y = 2.0;
 
 
			Point[0][0] = p1;
			Point[0][1] = p1;
			Point[0][2] = p1;
			Point[0][3] = p1;
			Point[0][4] = p1;
			Point[0][5] = p1;
			Point[0][6] = p1;
			Point[0][7] = p1;
			Point[0][8] = p1;
 
 
			Point[1][0] = p1;
			Point[1][1] = p1;
			Point[1][2] = p1;
			Point[1][3] = p1;
			Point[1][4] = p1;
			Point[1][5] = p1;
			Point[1][6] = p1;
			Point[1][7] = p1;
			Point[1][8] = p1;
 
 
			for (UINT i = 0; i < 8; i++)
			{
				for (UINT j = 0; j < 16; j++)
				{
					printf("%d\n", Point[i][j]);
				}
				printf("--------------i = %d\n", i);
			}
			DeletePOintArray(8, Point);
		}
	}
	else
	{
		// TODO:  更改錯誤程式碼以符合您的需要
		_tprintf(_T("錯誤:  GetModuleHandle 失敗\n"));
		nRetCode = 1;
	}
	return nRetCode;
}
 
 

二, C、C++之動態陣列的實現