数据结构 第19课 数组类的创建(上)------------------狄泰软件学院

前辈的文章按照课程顺序进行的,有了前辈的文章,省了好多事情,节约了很多时间,接下来只要粘贴代码即可,感谢前辈们

这里引用一下前辈的博客,两者相结合来学习

Array.h

#ifndef ARRAY_H
#define ARRAY_H

#include "Object.h"
#include "Exception.h"

namespace DTLib
{
    
    

template <typename T>
class Array : public Object
{
    
    
protected:
    T* m_array;
public:
    virtual bool set(int i, const T&e)    //O(1)   virtual
    {
    
    
        bool ret = ((0 <= i) && (i < length()));

        if( ret )
        {
    
    
            m_array[i] = e;
        }
        return 0;//不加这个会报错 --第20课
    }

    virtual  bool get(int i, T& e) const  //O(1)
    {
    
    
        bool ret = ((0 <= i) && (i < length()));

        if( ret )
        {
    
    
            e = m_array[i];
        }
        return 0;//  不加这个会报错 --第20课
    }

    T& operator[] (int i)   //O(1)
    {
    
    
        if((0 <= i) && (i < length()))
        {
    
    
            return m_array[i];
        }
        else
        {
    
    
            THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
        }
    }

    T operator[] (int i) const   //O(1)
    {
    
    
        return (const_cast< Array<T>&>(*this))[i];    // 用非const来实现const,强转换当前对象
    }

    virtual int length() const = 0;
};

}

#endif // ARRAY_H


StaticArray.h

/* 这个类是用来创建原生数组的替代品的,需要在定义一个数组对象的时候,可以直接指定这个数组对象当中的数据元素是什么类型
*  所以说必须是类模板;
*  原生的C++数组之间是不能相互的直接使用赋值操作符来进行复制的
*/

#ifndef STATICARRAY_H
#define STATICARRAY_H

#include "Array.h"

namespace DTLib
{
    
    

template <typename T, int N>
class StaticArray : public Array<T>
{
    
    
protected:
    T m_space[N];
public:
    StaticArray()   //O(1)
    {
    
    
        this->m_array = m_space;   //父类指针指向子类原生数组
    }

    StaticArray(const StaticArray<T, N>& obj)  //O(n)
    {
    
    
        this->m_array = m_space;

        for(int i = 0; i < N; i++)
        {
    
    
            m_space[i] = obj.m_space[i];  // 复制具体元素的值
        }
    }

    StaticArray<T, N>& operator= (const StaticArray<T, N>& obj)  //O(n)
    {
    
    
        if( this != &obj )
        {
    
    
            for(int i = 0; i < N; i++)
            {
    
    
                m_space[i] = obj.m_space[i]; // 参数对象中的值拷贝到当前对象中就可以
            }
        }

        return *this;
    }

    int length() const   // O(1)
    {
    
    
        return N;
    }
};

}

#endif // STATICARRAY_H

main.cpp

#include <iostream>
#include "StaticArray.h"


using namespace std;
using namespace DTLib;


int main()
{
    
    
    // 测试一
    StaticArray<int, 5> s1;

    for(int i = 0; i<s1.length(); i++)  // 现在数组自带了长度信息
    {
    
    
        s1[i] = i * i;
    }

    for(int i = 0; i < s1.length(); i++)
    {
    
    
        cout << s1[i] << endl;
    }



    // 测试二 : 支持拷贝
    StaticArray<int, 5> s2;

    s2 = s1;

    for(int i = 0; i < s2.length(); i++)
    {
    
    
        cout << s2[i] << endl;
    }


    // 测试三:  数组越界处理 (弥补了C++原生数组的缺陷)
    s2[6] = 100;


    // 测试四:  使用原生数组试一下,,,越界了,但是程序正常运行,,,这一行会出现问题,但是出现问题后什么时候表现出来是未知的
    int s3[5];
    s3[6] = 100;


    return 0;
}

猜你喜欢

转载自blog.csdn.net/dashuu/article/details/114706161