用c++实现数组类以代替原生数组,数组类可以包含数组的长度信息,主动发现访问越界,通过操作符重载还可以实现数组间的赋值赋值操作。类似于之前的线性表,数组类Array为一个抽象模板,数组的存储空间的分配和大小的指定均在子类实现。
数组空间由静态分配所得代码示例:
//Array.h
#ifndef __ARRAY_H__
#define __ARRAY_H__
#include <iostream>
#include <exception>
template<typename T>
class Array
{
public:
virtual bool set(int i, const T& e) //设置目的位置的元素值
{
bool ret = ((0 <= i) && (i < length()));
if (ret)
m_array[i] = e;
return ret;
}
virtual bool get(int i, T& e) //获取目的位置的元素值
{
bool ret = ((0 <= i) && (i < length()));
if (ret)
e = m_array[i];
return ret;
}
T& operator[](int i) //[]操作符重载函数,实现下标访问
{
if ((0 <= i) && (i < length()))
return m_array[i];
else
throw(std::out_of_range("Array::operator[] out of range"));
}
T& operator[](int i) const //[]操作符重载函数的const版本,供本类的const对象使用
{
return const_cast<Array<T>&>(*this)[i];
}
virtual int length() const = 0;
protected:
T* m_array;
};
#endif /* __ARRAY_H__ */
//StaticArray.h
#ifndef __STATICARRAY_H__
#define __STATICARRAY_H__
#include "Array.h"
template<typename T, int N>
class StaticArray : public Array<T>
{
protected:
T m_space[N]; //定义数组
public:
StaticArray()
{
this->m_array = m_space;
}
StaticArray(const StaticArray<T, N>& obj) //实现数组间的拷贝
{
for (int i = 0; i <N; i++)
m_space[i] = obj.m_space[i];
this->m_array = m_space;
}
StaticArray<T, N>& operator=(const StaticArray<T, N>& obj) //实现数组间的拷贝
{
if (this != &obj)
{
for (int i = 0; i < N; i++)
m_space[i] = obj.m_space[i];
}
return *this;
}
int length() const { return N; }
};
#endif /* __STATICARRAY_H__ */
//main.cpp
#include "StaticArray.h"
int main(void)
{
StaticArray<int, 6> p;
StaticArray<int, 6> p1;
for (int i = 0; i < 6; i++)
p[i] = i;
p1 = p;
for (int i = 0; i < 6; i++)
std::cout << p1[i] << std::endl;
getchar();
return 0;
}
数组空间由动态分配所得代码示例:
//DynamicArray.h
#ifndef __DYNAMICARRAY_H__
#define __DYNAMICARRAY_H__
#include "Array.h"
template<typename T>
class DynamicArray : public Array<T>
{
protected:
int m_length;
public:
DynamicArray(int length)
{
this->m_array = new T[length];
if (this->m_array)
{
this->m_length = length;
}
else
{
throw(std::out_of_range("DynamicArray::DynamicArray out of range"));
}
}
DynamicArray(const DynamicArray<T>& obj) //实现数组间的拷贝
{
this->m_array = new T[obj.m_length];
if (this->m_array)
{
this->m_length = obj.m_length;
for (int i = 0; i < obj.m_length; i++)
this->m_array[i] = obj.m_array[i];
}
else
throw(std::out_of_range("DynamicArray::DynamicArray out of range"));
}
DynamicArray<T>& operator=(const DynamicArray<T>& obj) //实现数组间的拷贝
{
if (this != &obj)
{
T* array = new T[obj.m_length];
if (array)
{
for (int i = 0; i < obj.m_length; i++)
{
array[i] = obj.m_array[i];
}
//注意异常安全
T* tmp = this->m_array;
this->m_array = array;
this->m_length = obj.m_length;
delete[] tmp;
}
else
throw(std::out_of_range("DynamicArray::DynamicArray out of range"));
}
return *this;
}
int length() const { return m_length; }
void resize(int length) /* 更改数组占据的内存空间 */
{
if (this->m_length != length)
{
T* array = new T[length];
if (array)
{
int sz = (length > this->m_length) ? this->m_length : length;
for (int i = 0; i < sz; i++)
array[i] = this->m_array[i];
//注意异常安全
T* tmp = this->m_array;
this->m_array = array;
this->m_length = length;
delete[] tmp;
}
else
throw(std::out_of_range("DynamicArray::resize out of range"));
}
}
~DynamicArray() { delete[] this->m_array; }
};
#endif /* __DYNAMICARRAY_H__ */
//main.cpp
#ifndef __DYNAMICARRAY_H__
#define __DYNAMICARRAY_H__
#include "Array.h"
template<typename T>
class DynamicArray : public Array<T>
{
protected:
int m_length;
public:
DynamicArray(int length)
{
this->m_array = new T[length];
if (this->m_array)
{
this->m_length = length;
}
else
{
throw(std::out_of_range("DynamicArray::DynamicArray out of range"));
}
}
DynamicArray(const DynamicArray<T>& obj) //实现数组间的拷贝
{
this->m_array = new T[obj.m_length];
if (this->m_array)
{
this->m_length = obj.m_length;
for (int i = 0; i < obj.m_length; i++)
this->m_array[i] = obj.m_array[i];
}
else
throw(std::out_of_range("DynamicArray::DynamicArray out of range"));
}
DynamicArray<T>& operator=(const DynamicArray<T>& obj) //实现数组间的拷贝
{
if (this != &obj)
{
T* array = new T[obj.m_length];
if (array)
{
for (int i = 0; i < obj.m_length; i++)
{
array[i] = obj.m_array[i];
}
//注意异常安全
T* tmp = this->m_array;
this->m_array = array;
this->m_length = obj.m_length;
delete[] tmp;
}
else
throw(std::out_of_range("DynamicArray::DynamicArray out of range"));
}
return *this;
}
int length() const { return m_length; }
void resize(int length) /* 更改数组占据的内存空间 */
{
if (this->m_length != length)
{
T* array = new T[length];
if (array)
{
int sz = (length > this->m_length) ? this->m_length : length;
for (int i = 0; i < sz; i++)
array[i] = this->m_array[i];
//注意异常安全
T* tmp = this->m_array;
this->m_array = array;
this->m_length = length;
delete[] tmp;
}
else
throw(std::out_of_range("DynamicArray::resize out of range"));
}
}
~DynamicArray() { delete[] this->m_array; }
};
#endif /* __DYNAMICARRAY_H__ */