版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、动态数组的C+实现动态数组在C+中有广泛的应用,但实现起来比较麻烦。这是我编写的一个动态数组的实现程序,程序的大部分功能都能实现,比如插入元素、向动态数组末尾追加元素、显示动态数组的大小、显示动态数组的容量、显示动态数组的元素。还有一些功能我虽然写了实现函数,但没有测试程序的结果,只写了最常见的几种操作。程序的最大优点是测试程序方便。程序代码/* * Array.cpp * * Created on: 2011-1-19 * Author: jiayanbo */#include#include #include #include using namespace std;/定义一个动态数组类c
2、lass Array int _size; int _capacity; int* items;public: typedef int* iterator; typedef const int* const_iterator; enum DEFAUL_CAP = 16 ; explicit Array(const int& capcity = DEFAUL_CAP); Array(const Array& other); Array& operator=(const Array& other); Array(); iterator begin(); const_iterator begin()
3、 const; iterator end(); const_iterator end() const; const int& operator(const int& index) const; int& operator(const int& index); void insert(const int& index, const int& value); void remove(const int& index); void append(const int& value); int size() const; int capacity() const; bool empty() const;
4、 void run();/构造函数Array:Array(const int& capcity) : _size(0), _capacity(capcity) items = new int_capacity;/copy构造函数Array:Array(const Array& other) : _size(other._size), _capacity(other._capacity) items = new int_capacity; /for (int i = 0; i _size; +i) / itemsi = other.itemsi; std:memcpy(items, other.
5、items, _size * sizeof(int);/运算符=重载Array& Array:operator=(const Array& other) if (&other != this) _size = other._size; _capacity = other._capacity; delete items; items = new int_capacity; std:memcpy(items, other.items, _size * sizeof(int); return *this;/查找数组开始位置Array:iterator Array:begin() return ite
6、ms;/查找数组开始位置(不能改变)Array: const_iterator Array:begin() const return items;/查找数组结束位置 Array:iterator Array:end() return items + _size;/查找数组结束位置(不能改变)Array:const_iterator Array:end() const return items + _size;/查找数组中index下标的元素const int& Array:operator(const int& index) const if (index 0 | _size = index)
7、 throw std:out_of_range(Array:operator(const int&) const); return itemsindex;/查找数组中index下标的元素(不能改变其大小)int& Array:operator(const int& index) if (index 0 | _size = index) throw std:out_of_range(Array:operator(const int&) const); return itemsindex;/在动态数组中插入元素void Array:insert(const int& index, const in
8、t& value) if (index 0 | _size = index) throw std:out_of_range(Array:insert(const int&, const int&); if (_size _capacity) std:memmove(items + (index + 1), items + index, (_size - index) * sizeof(int); itemsindex = value; else _capacity *= 2; int* tmp = new int_capacity; std:memcpy(tmp, items, index *
9、 sizeof(int); std:memcpy(tmp + (index + 1), items + index, (_size - index)* sizeof(int); tmpindex = value; delete items; items = tmp; +_size;/在动态数组末尾追加元素void Array:append(const int& value) if (_size _capacity) items_size = value; else _capacity *= 2; int* tmp = new int_capacity; std:memcpy(tmp, item
10、s, _size * sizeof(int); tmp_size = value; delete items; items = tmp; +_size;/删除动态数组的元素void Array:remove(const int& index) if (index 0 | _size = index) throw out_of_range(Array:remove(const int&); -_size; if (index != _size) / _size already decreased. memmove(items + index, items + (index + 1), (_siz
11、e - index) * sizeof(int);/ for (int i = index; i _size; +i)/ itemsi = itemsi + 1;/查找动态数组的大小int Array:size() const return _size;/查找动态数组的容量int Array:capacity() const return _capacity;/判断动态数组是否为空bool Array:empty() const return 0 = _size;/运行函数void Array:run()char key;int i,num,value;cout请输入你的选择:key;whil
12、e(key!=0) switch(key)case 1:coutnum;coutkey;insert(num, key);break;case 2:coutkey;append(key);break;case 3:cout动态数组的大小为:tsize()endl;break;case 4:cout动态数组的容量为:tcapacity()endl;break;case 5:coutnum;remove(num);break;default:break;cout请输入你的选择:key;/析构动态数组Array:Array() delete items;int main() Array a(10);
13、cout*请输入你想对动态数组的操作:(按0退出程序)*endl;cout*1、插入元素,2、追加元素,3、查看数组的大小,*endl;cout*4、查看数组的容量,5、删除元素*endl;a.run();运行结果:1、*请输入你想对动态数组的操作:(按0退出程序)*1、插入元素,2、追加元素,3、查看数组的大小,*4、查看数组的容量,5、删除元素、6显示动态数组中的元素*请输入你的选择:1请输入插入元素的位置:2请输入插入元素的值:2terminate called after throwing an instance of std:out_of_range what(): Array:insert(const int&, const int&)已放弃2、*请输入你想对动态数组的操作:(按0退出程序)*1、插入元素,2、追加元素,3、查看数组的大小,*4、查看数组的容量,5、删除元素、6显示动态数组中的元素*请输入你的选择:2请输入追加元素的值:6请输入你的选择:2请输入追加元素的值:8请输入你的选择:2请输入追加元素的值:7请输入你的选择:2请输入追加元素的值:3请输入你的选择:1请输入插入元素的位置:2请
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 告别粗心失分|高三生物现代生物进化暑假易错题型清零课件
- AI与传统书法文化的数字化创新与发展
- 2025安徽芜湖城市园林集团股份有限公司招聘招聘30人笔试历年常考点试题专练附带答案详解
- 2025国核电力规划设计研究院有限公司招聘4人笔试历年备考题库附带答案详解
- 2025四川广安岳池县特岗特聘“国企经理人”招聘10人笔试历年难易错考点试卷带答案解析
- 2025四川南充市公共交通有限责任公司招聘公交车驾驶员20人笔试历年典型考点题库附带答案详解
- 2025咸阳经开城市发展集团有限公司招聘(21人)笔试历年难易错考点试卷带答案解析
- 2025华晋焦煤井下岗位高校毕业生招聘260人(山西)笔试历年常考点试题专练附带答案详解
- 2025内蒙古放歌文化传媒有限公司招聘26人笔试历年难易错考点试卷带答案解析
- 2025内蒙古乌海包钢矿业公司招聘招聘29人笔试参考题库附带答案详解
- 低压用电系统漏电保护技术措施培训
- 2026角膜塑形镜市场渗透率区域差异与青少年近视防控政策关联
- 留疆战士考试题及答案
- 2026化学高考广西考试真题及答案
- 合规岗位招聘笔试题及解答(某大型国企)2025年
- 2026南方凯能(广东)电力集团校园招聘考试参考试题及答案解析
- 2026年救生舱行业分析报告及未来发展趋势报告
- 2026年甘肃高考历史真题试卷含答案
- 2026年比亚迪AI视频面试题库与答题技巧
- 2026年青岛国际机场集团有限公司校园招聘笔试备考试题及答案解析
- DB15∕T 4258-2026 草种子生产基地建设技术规程
评论
0/150
提交评论