首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器admin管理员组文章数量:1566220
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
|
|
C++编程实现变长数组
博客 佚名 2009-3-20 保存本文 推荐给好友 收藏本页 欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入1.变长一维数组
这里说的变长数组是指在编译时不能确定数组长度,程序在运行时需要动态分配内存空间的数组。实现变长数组最简单的是变长一维数组,你可以这样做:
//文件名: array01.cpp int main() |
注意int *p=new int[len];这一句,你不能这样做:
int p[len];
C++编译器会报错说len的大小不能确定,因为用这种形式声明数组,数组的大小需要在编译时确定。而且这样也不行:
int p[]=new int[len];
编译器会说不能把int*型转化为int[]型,因为用new开辟了一段内存空间后会返回这段内存的首地址,所以要把这个地址赋给一个指针,所以要用int *p=new int[len];
array01.cpp实现了一个变长的一维数组,但是要养成一个好习惯,就是注意要注销指针p,使程序释放用new开辟的内存空间。
当然使用C++标准模版库(STL)中的vector(向量)也可以实现变长数组:
//文件名: array02.cpp int main() for(int i=0;i<len;i++) |
这里的变长数组让我联想到了java的java.util包中的vector和C#中的ArrayList,它们也可以在各自的语言中实现变长数组。不过C++中的vector不能像C#一样有托管的垃圾回收机制回收被占用的内存空间,但是你可以在使用完vector后调用~vector()析构函数释放内存。
2.变长n维数组
变长的n维数组实现起来有些麻烦,但是在工程与软件设计应用中常使用的是二维数组,所以在这里着重介绍变长的二维数组,变长的n维数组可以按照类似的方法实现。首先看一个经典的用C实现变长二维数组的例子:
//文件名: array03.c void main() { a=(float **)malloc(sizeof(float *) *x); /*读入数据*/ printf("请按行的顺序依次输入系数的值(共%d项):",x*y); printf("您输入方程组的增广矩阵为:\n"); |
那么用C++怎样实现呢?在C++中可以通过new和delete运算符动态开辟和释放空间,其中new与C中malloc函数的功能相似,delete与C中free函数的功能相似。用C++实现变长二维数组时可以采用两种方法:双指针方法和使用STL中vector(向量)的方法。
首先介绍一下双指针方法,在这里双指针就是指像指针的指针,比如你可以这样声明一个数组:
int **p = new int*[num1];
而对每一个*p(一共num1个*p)申请一组内存空间:
for(int i=0; i<num1; ++i)
p[i] = new int[num2];
其中,num1是行数,num2是数组的列数。测试的源程序如下:
//文件名: array04.cpp int main() cout<<"Please enter the number for row and column: "<<endl; //为二维数组开辟空间 for(int j=0;j<num1;j++) //释放二维数组占用的空间 return 0; |
以下是运行结果:
Please enter the number for row and column:
4 5
1:004915F0 2:004915F4 3:004915F8 4:004915FC 5:00491600
2:00491180 4:00491184 6:00491188 8:0049118C 10:00491190
3:00491140 6:00491144 9:00491148 12:0049114C 15:00491150
4:00491100 8:00491104 12:00491108 16:0049110C 20:00491110
Press any key to continue
程序清单array04.cpp可以显示分配的内存空间单元的地址,大家可以看到,由于数组空间是动态分配的,数组行之间的地址空间是不连续的,因为不同行的数组元素的地址空间是用不同的new来分配的。而每一行之中列之间的地址空间是连续的。
那么用vector(向量)怎样实现二维数组呢?以下给出源程序:
//文件名: array05.cpp cout << "input value for m,n:"; //注意下面这一行:vector<int后两个">"之间要有空格!否则会被认为是重载">>"。 for (i = 0; i < m; i++)
|
以下是运行结果:
input value for m,n:3 4
0: 00491180 0: 00491184 0: 00491188 0: 0049118C
0: 00491140 1: 00491144 2: 00491148 3: 0049114C
0: 00491100 2: 00491104 4: 00491108 6: 0049110C
Press any key to continue
大家可以看到,这里vector中元素的内存的地址分配也有同双指针实现的二维数组有同样的特点。不过用vector的方法比使用双指针简单地多,分配内存空间时会更安全,数组初始化代码也更简单,所以本人建议使用STL中的vector来实现变长多维数组。以下是一个变长三维数组:)
//文件名: array06.cpp cout << "input value for m,n,l:"; for (i = 0; i < m; i++) return 0;
|
运行结果:
input value for m,n,l:2 3 4
0: 00492FE0 1: 00492FE4 2: 00492FE8 3: 00492FEC
1: 00492FA0 2: 00492FA4 3: 00492FA8 4: 00492FAC
2: 00492F60 3: 00492F64 4: 00492F68 5: 00492F6C
1: 00492EC0 2: 00492EC4 3: 00492EC8 4: 00492ECC
2: 00492E80 3: 00492E84 4: 00492E88 5: 00492E8C
3: 00492E40 4: 00492E44 5: 00492E48 6: 00492E4C
相关文章 C/C++双链表正排序
C/C++小写数字转换成大写数字
高精度实现10000位数字的乘除法(C++)
约瑟夫环问题求解算法C语言源代码
C++三种排序算法实例代码
C语言实现GBK/GB2312/五大码之间的转换
C++查找字符在字符串中出现的次数
c++ win32 API实现方块联机游戏历程
C语言判断素数并输出
C语言正整数分解质因数
编辑推荐 · [ ASP.NET] .NET程序员新方向 Ruby核心语法入门
· [ ASP.NET] 基于ASP.NET MVC框架开发Web论坛应用程序
· [ ASP.NET] 利用UrlRewrite,asp动态生成htm页面
· [ .NET Framework] DotNet for Symbian平台
· [ ASP.NET] WCF 关于自定义MessageHeader支持
· [ ASP.NET] 如何定制页面提示信息(tooltips)
· [ ASP.NET] 对IOC和DI的理解
· [ ASP.NET] 主动编程与被动编程!
· [ ASP.NET] 发布一个锁定行列的一种方法
· [ ASP.NET] 使用配置文件构建和使用WCF服务
width="660" height="213" src="http://dotnet.chinaitlab/cio_new.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"> 相关产品和培训 width="660" height="159" src="http://java.chinaitlab/peixun_new.htm" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"> 文章评论 width="100%" height="330" src="http://www.chinaitlab/CMS2006_Comment/Comment/ArticleComment.asp?ID=779584" frameborder="0" scrolling="yes"> 友情推荐链接 · 华硕网络交换机
· 金浪网络交换机
· 联想交换机报价
· 联想交换机价格
· lenovo交换机
· IDC资讯大全
· 机房品质万里行
· IDC托管必备知识
· 网站推广优化
· 全国IDC报价
认证培训 name="aswift_1" width="264" height="250" id="aswift_1" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" vspace="0" hspace="0" allowfullscreen="true" style="left: 0px; top: 0px; position: absolute;" allowtransparency="true"> 专题推荐
· Oracle视图 优化数据库管理
· C语言之水滴石穿
· C/C++ 穷举法 运用方法与实例
· .NET开发十大必备工具
· Java多线程编程 基础到深入教程
· Java网络编程-基础到进阶教程
· C++箴言大合集
· 开源框架 Structs 2.0入门教程
· 诱人的奶酪 J2ME手机开发技术
· Oracle外键及外键约束修改行为
今日更新 · 用Visual C++编程实现局域网多播
· C++调用MATLAB引擎
· 在C++中如何 使用调试器逐步跟踪程序
· C++惯用法之RAII
· 基础知识:C/C++ 常见误区
· 从0开始学习linux C编程
· C/C++ 误区五:检查 new 的返回值
· C/C++ 误区四:char c = getchar();
· C/C++ 误区三:强制转换 malloc() 的返回值
· C/C++ 误区二:fflush(stdin)
社区讨论 width="265" height="188" src="http://dotnet.chinaitlab/bbs.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"> 博客论点 width="265" height="188" src="http://dotnet.chinaitlab/blog.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"> 频道精选 · 新版CCNA考试大纲 07年8月1日生效
· 新手学习宝典:Linux常用命令全集
· 知已知彼,深入了解系统安全知识
· 从入门到精通 java初学者实践系列教程
· 共同学习——Oracle入门基础专题
· ADO与PowerBuilder的综合比较
· 评论:中国互联网“钱”途何在?
· 中科院:龙芯要成"中国奔腾" 能卖1亿颗
C/C++频道相关导航 C语言:
基础入门 | 编程技巧 | 实例编程
C++编程:
基础入门 | 实例编程 | 编程技巧
C++开发应用:
多媒体开发 | 游戏开发 | 数据库开发 | 网络通信
组件编程 | 系统应用 | 界面开发
资讯动态 :
VC++:
C++Builder:
687
版权声明:本文标题:C++编程实现变长数组 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726387998a1068333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论