首页 > 代码库 > c++实现的Array数据结构

c++实现的Array数据结构

1.Array.h,Array<T>的定义

template <class T>class Array{protected:    T *data;    //一个指向数组数据的指针    unsigned int base;    //base为数组的起始下表    unsigned int length;    //length为数组的长度public:    Array();    //缺省的构造函数    Array(unsigned int, unsigned int = 0);    //数组构造函数    ~Array();    //析构函数    Array(Array const&);    //拷贝构造函数    Array& operator = (Array const&);    //重载等号操作符,用于一个数组给另外一个数组赋值    T const& operator [] (unsigned int) const;    //重载中括号操作符,返回一个T数值常量,返回值不能被改变,在函数末尾加const表示this指针指向const    T& operator [] (unsigned int);    //重载中括号操作符,返回一个T数值常量,其返回值可以被改变    T* Data() const;    //返回数组数据的指针data    unsigned int Base() const;    //返回成员base    unsigned int Length() const;    //返回成员length    void SetBase(unsigned int);    //设置成员变量base的数值    void SetLength(unsigned int);    //设置成员变量length的数值};//动态数组所占空间S(n)=sizeof(T*)+2sizeof(unsigned int)+nsizeof(T),假定T类型所占用空间为一个常数,故S(n)=O(n)

 

2.Array<T>中成员函数的实现

#include "Array.h"template <class T>Array<T>::Array() :    data(new T[10]),     base(0),    length(0){}//缺省的构造函数不含变量,只需要给对象的变量一个初始值,时间复杂度O(1)template <class T>Array<T>::Array(unsigned int n, unsigned int m) :    data(new T[n]),    base(m),     length(n){}//初始化数组,n为数组的长度,时间复杂度常量O(1)template <class T>Array<T>::Array(Array<T> const& array) :    data(new T[array.length]),    base(array.base),    length(array.length){    for (unsigned int i = 0; i < length; ++i)        data[i] = array.data[i];}//备份构造函数,将一个数组从赋值到另外一个数组,时间复杂度为O(n)template <class T>Array<T>::~Array(){    delete[] data;}//析构函数,删除数组所占用的内存空间template <class T>T* Array<T>::Data() const{    return data;}template <class T>unsigned int Array<T>::Base() const{    return base;}template <class T>unsigned int Array<T>::Length() const{    return length;}//这三个为存取器函数,用来返回成员,时间复杂度都为O(1)template <class T>T const& Array<T>::operator[] (unsigned int position) const{    unsigned int const offset = position - base;    if (offset >= length)        throw out_of_range("invalid position");    return data[offset];}template <class T>T& Array<T>::operator[] (unsigned int position){    unsigned int const offset = position - base;    if (offset >= length)        throw out_of_range("invalid position");    return data[offset];}//这两个都为取下表操作符的重载,区别是第一个返回值不可以作为左值,第二个返回值可以作为左值,时间复杂度都为O(1)template <class T>void Array<T>::SetBase(unsigned int newBase){    base = newBase;}template <class T>void Array<T>::SetLength(unsigned int newLength){    T* const newData = http://www.mamicode.com/new T[newLength];    unsigned int const min = length < newLength ? length : newLength;    for (unsigned int i = 0; i < min; ++i)        newData[i] = data[i];    delete[] data;    data = newData;    length = newLength;}//这两个函数来重设对象的成员,时间复杂度为T(m,n)=min(m,n)*T(T::T(T&))+O(1)template <class T>Array<T>& Array<T>::operator = (Array<T> const& array){    if (this != &array)    {        delete[] data;        base = array.base;        length = array.length;        data = new T[length];        for (unsigned int i = 0; i < length; ++i)            data[i] = array.data[i];    }    return this;}//重载赋值操作符,时间复杂度为O(n)

 

3.测试主函数main.cpp

#include "Array.cpp"#include <iostream>using namespace std;template <class T> void Output(Array<T> array);template <class T>void Output(Array<T> array){    cout << "data:";    for (unsigned int i = array.Base(); i < array.Length(); i++)    {        cout << array.Data()[i] << " ";    }    cout << endl;    cout << "length:" << array.Length()<<endl;    cout << "base:" << array.Base() <<endl;}int main(){    cout << "Array()正在执行。。。" << endl;    Array<int> array0 = Array<int>();    Output(array0);    cout << "Array(unsigned int, unsigned int = 0)正在执行。。。" << endl;    Array<int> array1 = Array<int>(10);    Output(array1);    cout << "Array(Array const&)正在执行。。。" << endl;    Array<int> array2(array1);    Output(array2);    cout << "~Array()正在执行。。。" << endl;    array2.~Array();    Output(array2);    cout << "T const* Data() const,unsigned int Base() const,unsigned int Length() const,"        << "T const& operator [] (unsigned int) const在Output函数中执行。。。" << endl;    cout << "T& operator [] (unsigned int)正在执行。。。" << endl;    Array<int> array3(10);    for (unsigned int i = array1.Base(); i < array1.Length() - array1.Base(); i++)    {        array3.Data()[i] = i;    }    Output(array3);    cout << "void SetBase(unsigned int)正在执行。。。" << endl;    array3.SetBase(2);    Output(array3);    cout << "void SetLength(unsigned int)正在执行。。。" << endl;    array3.SetLength(7);    Output(array3);    getchar();    return 0;}

 

4.测试结果

 

c++实现的Array数据结构