动态数组的C+实现_第1页
动态数组的C+实现_第2页
动态数组的C+实现_第3页
动态数组的C+实现_第4页
动态数组的C+实现_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论