C++中如何定义动态数组

C++中如何定义动态数组C 中如何定义动态数组 http blog csdn net bzhxuexi article details 标签 nbsp C 动态数组 2013 12 0921 27 nbsp 3145 人阅读 nbsp 评论 0 nbsp 收藏 nbsp 举报 nbsp 分类 C C 105 nbsp 首先 为什么需要动态定义数组呢 nbsp 这是因为 很多情况下 在预编

C++中如何定义动态数组

http://blog.csdn.net/bzhxuexi/article/details/

标签: C++动态数组
 
3145人阅读 
评论(0) 
收藏 
举报
C++中如何定义动态数组  分类:

———————————–

 

例子:

 

   size =6;

 

   column =5

 

   int p=new int*[size];

 

    for(int i=0;i
   {


     p[i]=new int[Column];
   }






 

 

 

所生成的动态数组如下图所示:

 

 

C++中如何定义动态数组 
   






  最后 ,因为调用了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

(0)
上一篇 2026年3月18日 下午9:22
下一篇 2026年3月18日 下午9:23


相关推荐

  • 拉格朗日乘子法以及KKT条件

    拉格朗日乘子法以及KKT条件

    2021年11月19日
    61
  • Android性能优化方案

    Android性能优化方案保证性能指标不下降一直是开发过程中的重中之重 如果由于开发新功能导致出现卡顿 机身发热耗电量猛增 内存增大等性能问题 那样反而会流失用户 得不偿失 因此关注性能也是 RD 们的一项隐形工作

    2026年3月19日
    2
  • 一个汉字是几个字节

    一个汉字是几个字节一个汉字是几个字节常用中文字符用 utf 8 编码占用 3 个字节 大约 2 万多字 但超大字符集中的更大多数汉字要占 4 个字节 在 unicode 编码体系中 U 20000 开始有 5 万多汉字 GBK GB2312 收编的汉字占 2 个字节 严格地用 iso8859 1 无法表示汉字 只能转为问号 代码演示如下 publicstatic String args throwsUnsupp

    2026年3月19日
    3
  • 数据库常用sql语句总结

    数据库常用sql语句总结查看时右侧可以打开CSDN自带的目录,方便查看目录一、基础1.SELECT语句2.SELECTDISTINCT语句3.WHERE子句4.AND和OR运算符5.ORDERBY语句6.INSERTINTO语句7.Update语句8.DELETE语句二、高级1.TOP子句2.LIKE操作符3.SQL通配符4.IN…

    2022年6月22日
    36
  • 与运算或运算非运算异或运算是什么_俄称击退乌军进攻

    与运算或运算非运算异或运算是什么_俄称击退乌军进攻按位与运算符(&)参加运算的两个数据,按二进制位进行“与”运算。运算规则:0&0=0;  0&1=0;   1&0=0;    1&1=1;      即:两位同时为“1”,结果才为“1”,否则为0例如:3&5 即00000011&00000101=00000001  因此,3&5的值得1。 另,负数按补码形式参加按位与运算。“与运算”的特殊用途:(1

    2025年6月8日
    5
  • 博弈论集锦

    博弈论集锦看完这么多效应及定理,能否总结成一条?1.马太效应   《新约马太福音》中有这样一个故事,一个国王远行前,交给三个仆人每人一锭银子,吩咐他们:“你们去做生意,等我回来时,再来见我。”国王回来时,第一个仆人说:“主人,你交给我们的一锭银子,我已赚了10锭。”于是国王奖励他10座城邑。第二个仆人报告说:“主人,你给我的一锭银子,我已赚了5锭。”于是国王例奖励了他5座城邑。第三个

    2022年5月4日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号