3.数组类创建

 

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)
	{
		bool ret = ((0 <= i) && (i < length()));
		if (ret)
		{
			m_array[i] = e;
		}
		return ret;
	}

	virtual bool get(int i, T& e) const
	{
		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_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
		}
	}
	T operator[] (int i) const
	{
		return (const_cast<Array<T>&>(*this))[i];
	}

	virtual int length() const = 0;
};



}

#endif // !__ARRAY_H

StaticArray.h

#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()
	{
		this->m_array = m_space;
	}
	StaticArray(const StaticArray<T, N>& obj)
	{
		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)
	{
		if (this != &obj)
		{
			for (int i = 0; i < N; i++)
			{
				m_space[i] = obj.m_space[i];
			}
		}
		else
		{
			return *this;
		}
	}
	int length() const
	{
		return N;
	}

};


}


#endif // ! __STATICARRAY_H

测试main.cpp

#include <iostream>
//#include "SmartPointer.h"
//#include"Exception.h"
//#include"Object.h"
//#include "StaticList.h"
//#include "DynamicList.h"
#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;
	}
	//异常测试
	//s2[6] = 100;
	
	return 0;
}

 

 

​
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include "Array.h"
#include "Exception.h"

namespace DTLib
{

    template < typename T >
    class DynamicArray : public Array<T>
    {
    protected:
        int m_length;

        T* copy(T* array, int len, int newLen)  // O(min(len, newLen)) ==> O(n)
        {
            T* ret = new T[newLen];

            if (ret != NULL)
            {
                int size = (len < newLen) ? len : newLen;

                for (int i = 0; i < size; i++)
                {
                    ret[i] = array[i];
                }
            }

            return ret;
        }

        void update(T* array, int length) //  O(1)
        {
            if (array != NULL)
            {
                T* temp = this->m_array;

                this->m_array = array;
                this->m_length = length;

                delete[] temp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No memory to update DynamicArray object ...");
            }
        }

        void init(T* array, int length)  // O(1)
        {
            if (array != NULL)
            {
                this->m_array = array;
                this->m_length = length;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create DynamicArray object ...");
            }
        }

    public:
        DynamicArray(int length = 0)  // O(1)
        {
            init(new T[length], length);
        }

        DynamicArray(const DynamicArray<T>& obj)  // O(n)
        {
            init(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);
        }

        DynamicArray<T>& operator= (const DynamicArray<T>& obj)  // O(n)
        {
            if (this != &obj)
            {
                update(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);
            }

            return *this;
        }

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

        void resize(int length)  // O(n)
        {
            if (length != m_length)
            {
                update(copy(this->m_array, m_length, length), length);
            }
        }

        ~DynamicArray()   // O(1)
        {
            delete[] this->m_array;
        }
    };


}

#endif // DYNAMICARRAY_H

​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值