一个动态数组的例子C++
2017-04-21 18:34
351 查看
C/C++ 规定,数组一旦定义后,它的长度就不能改变了;换句话说,数组容量不能动态地增大或者减小。这样的数组称为静态数组(Static
array)。静态数组有时候会给编码代码不便,我们可以通过自定义的 Array 类来实现动态数组(Dynamic
array)。所谓动态数组,是指数组容量能够在使用的过程中随时增大或减小。
下面这段代码虽然有点长,但它是一个典型的使用异常的场景,请大家耐心阅读。
运行结果:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Error: out of range( array length 10, access index 20 )
Error: empty array, no elements to pop.
Empty array! No elements to print.
Array
类实现了动态数组,它的主要思路是:在创建对象时预先分配出一定长度的内存(通过 malloc() 分配),内存不够用时就再扩展内存(通过 realloc() 重新分配)。Array 数组只能在尾部一个一个地插入(通过 push() 插入)或删除(通过 pop() 删除)元素。
我们通过重载过的
array)。静态数组有时候会给编码代码不便,我们可以通过自定义的 Array 类来实现动态数组(Dynamic
array)。所谓动态数组,是指数组容量能够在使用的过程中随时增大或减小。
下面这段代码虽然有点长,但它是一个典型的使用异常的场景,请大家耐心阅读。
#include <iostream> #include <cstdlib> using namespace std; //自定义的异常类型 class OutOfRange{ public: OutOfRange(): m_flag(1){ }; OutOfRange(int len, int index): m_len(len), m_index(index), m_flag(2){ } public: void what() const; //获取具体的错误信息 private: int m_flag; //不同的flag表示不同的错误 int m_len; //当前数组的长度 int m_index; //当前使用的数组下标 }; void OutOfRange::what() const { if(m_flag == 1){ cout<<"Error: empty array, no elements to pop."<<endl; }else if(m_flag == 2){ cout<<"Error: out of range( array length "<<m_len<<", access index "<<m_index<<" )"<<endl; }else{ cout<<"Unknown exception."<<endl; } } //实现动态数组 class Array{ public: Array(); ~Array(){ free(m_p); }; public: int operator[](int i) const; //获取数组元素 int push(int ele); //在末尾插入数组元素 int pop(); //在末尾删除数组元素 int length() const{ return m_len; }; //获取数组长度 private: int m_len; //数组长度 int m_capacity; //当前的内存能容纳多少个元素 int *m_p; //内存指针 private: static const int m_stepSize = 50; //每次扩容的步长 }; Array::Array(){ m_p = (int*)malloc( sizeof(int) * m_stepSize ); m_capacity = m_stepSize; m_len = 0; } int Array::operator[](int index) const { if( index<0 || index>=m_len ){ //判断是否越界 throw OutOfRange(m_len, index); //抛出异常(创建一个匿名对象) } return *(m_p + index); } int Array::push(int ele){ if(m_len >= m_capacity){ //如果容量不足就扩容 m_capacity += m_stepSize; m_p = (int*)realloc( m_p, sizeof(int) * m_capacity ); //扩容 } *(m_p + m_len) = ele; m_len++; return m_len-1; } int Array::pop(){ if(m_len == 0){ throw OutOfRange(); //抛出异常(创建一个匿名对象) } m_len--; return *(m_p + m_len); } //打印数组元素 void printArray(Array &arr){ int len = arr.length(); //判断数组是否为空 if(len == 0){ cout<<"Empty array! No elements to print."<<endl; return; } for(int i=0; i<len; i++){ if(i == len-1){ cout<<arr[i]<<endl; }else{ cout<<arr[i]<<", "; } } } int main(){ Array nums; //向数组中添加十个元素 for(int i=0; i<10; i++){ nums.push(i); } printArray(nums); //尝试访问第20个元素 try{ cout<<nums[20]<<endl; }catch(OutOfRange &e){ e.what(); } //尝试弹出20个元素 try{ for(int i=0; i<20; i++){ nums.pop(); } }catch(OutOfRange &e){ e.what(); } printArray(nums); return 0; }
运行结果:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Error: out of range( array length 10, access index 20 )
Error: empty array, no elements to pop.
Empty array! No elements to print.
Array
类实现了动态数组,它的主要思路是:在创建对象时预先分配出一定长度的内存(通过 malloc() 分配),内存不够用时就再扩展内存(通过 realloc() 重新分配)。Array 数组只能在尾部一个一个地插入(通过 push() 插入)或删除(通过 pop() 删除)元素。
我们通过重载过的
[ ]运算符来访问数组元素,如果下标过小或过大,就会抛出异常(第53行代码);在抛出异常的同时,我们还记录了当前数组的长度和要访问的下标。
相关文章推荐
- C++ 申请一个动态三维数组
- 4.4 c++ 上机 写一个动态的数组类
- C++ 动态申请数组的一个Demo
- 一个 C++ 中动态的插入、删除数据的例子.
- 【C++学习】通过输入数来确定一个数组的动态大小
- 上机实践 - - 一个例子了解C/C++中指针与数组的区别
- c++自定义一个动态数组vector
- 初步的学会用数组动态添加参数(一个添加存储过程参数的例子)
- 数组不是指针——数组地址不能动态分配空间,一个小例子关于指针移动,以及malloc
- DataGrid动态添加模板列的一个例子
- .net 2.0学习动态语言的另外一个例子?
- DataGrid动态添加模板列的一个例子
- C++ 用new 动态创建多维数组
- (转贴)一个C#动态编译的小例子
- C++ Boost 之Python(一个简单的例子)
- C++ 动态建立数组
- 如何动态创建一个数组
- 今天要为自己的网站添加一个动态检索信息的下拉提示框,这个是在网上找的例子。
- DataGrid动态添加模板列的一个例子
- 一个动态生成脚本的例子