动态数组的C++实现
动态数组在C++中有广泛的应用,但实现起来比较麻烦。这是我编写的一个动态数组的实现程序,程序的大部分功能都能实现,比如插入元素、向动态数组末尾追加元素、显示动态数组的大小、显示动态数组的容量、显示动态数组的元素。还有一些功能我虽然写了实现函数,但没有测试程序的结果,只写了最常见的几种操作。程序的最大优点是测试程序方便。
程序代码
/*
* Array.cpp
*
* Created on: 2011-1-19
* Author: jiayanbo
*/
#include<iostream>
#include <stdexcept>
#include <cstring>
#include <iterator>
using namespace std;
//定义一个动态数组类
class 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() 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;
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)
// items[i] = other.items[i];
std::memcpy(items, other.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 items;
}
//查数组开始位置(不能改变)
Array:: const_iterator Array::begin() const {
return items;
}
//查数组结束位置
Array::iterator Array::end() {
return items + _size;
}
cstring转为int//查数组结束位置(不能改变)
Array::const_iterator Array::end() const {
return items + _size;
}
//查数组中index下标的元素
const int& Array::operator[](const int& index) const {
if (index < 0 || _size <= index)
throw std::out_of_range("Array::operator[](const int&) const");
return items[index];
}
//查数组中index下标的元素(不能改变其大小)
int& Array::operator[](const int& index) {
if (index < 0 || _size <= index)
throw std::out_of_range("Array::operator[](const int&) const");
return items[index];
}
//在动态数组中插入元素
void Array::insert(const int& index, const int& 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));
items[index] = value;
} else {
_capacity *= 2;
int* tmp = new int[_capacity];
std::memcpy(tmp, items, index * sizeof(int));
std::memcpy(tmp + (index + 1), items + index, (_size - index)* sizeof(int));
tmp[index] = 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, items, _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), (_size - index) * sizeof(int));
// for (int i = index; i < _size; ++i)
// items[i] = items[i + 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<<"请输入你的选择:"<<"\t";
cin>>key;
while(key!='0')
{
switch(key)
{
case '1':{
cout<<"请输入插入元素的位置:\t";
cin>>num;
cout<<"请输入插入元素的值:\t";
cin>>key;
insert(num, key);
break;
}case '2':{
cout<<"请输入追加元素的值:\t";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论