C++中如何定义动态数组
http://blog.csdn.net/bzhxuexi/article/details/
分类: ———————————–
例子:
size =6;
column =5
int p=new int*[size];
for(int i=0;i
p[i]=new int[Column];
}
所生成的动态数组如下图所示:
最后 ,因为调用了new, 千万千万别忘记在用完之后,将其所占资源 delete 掉
下面是delete方法:
for(int i=0;i
delete [] p; //最后不要忘掉 释放掉开辟的指针数组 :》
// ArrayTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "ArrayTest.h" #include #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; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211879.html原文链接:https://javaforall.net
