C++写的数组模板类

#ifndef CARRAY_H
#define CARRAY_H


#include<iostream>
using namespace std;

template<class Type>
class CArray {

public:
    //CArray();
    CArray(int length);
    CArray(Type* initArray, const int length);
    ~CArray();
    int length();
    Type min();
    Type max();
    Type operator[](const int index) const;
    bool operator==(const CArray<Type>&) const;
    bool operator=(const CArray<Type>&) const;

private:
    int m_Length;
    Type *p_array;
};

//构造函数1,为数组分配内存
template<class Type>
CArray<Type>::CArray(int length)
{
    p_array = new Type[length];
    m_Length = length;
}

//构造函数2,使用已有数组初始化该数组
template<class Type>
CArray<Type>::CArray(Type* initArray, const int length)
{
    p_array = new Type[length];
    m_Length = length;
    for(int i=0;i<m_Length;i++)
    {
        p_array[i] = initArray[i];
    }
}

//析构函数,删除对象分配的类存
template<class Type>
CArray<Type>::~CArray()
{
    if(p_array != NULL)
    {
        delete[] p_array;
    }
}

//成员函数,返回数组长度信息
template<class Type>
int CArray<Type>::length()
{
    if(p_array != NULL)
    {
        return m_Length;
    }
    else
    {
        return 0;
    }
}

//返回数组的最小值
template<class Type>
Type CArray<Type>::min()
{
    Type minimum;
    if(p_array != NULL)
    {
        minimum = p_array[0];
        for(int i=1;i<m_Length;i++)
        {
            if(p_array[i]<minimum)
                minimum = p_array[i];
        }
        return minimum;
    }
    else
        return 0;
}

//返回数组的最大值
template<class Type>
Type CArray<Type>::max()
{
    Type maximum;
    if(p_array != NULL)
    {
        maximum = p_array[0];
        for(int i=1;i<m_Length;i++)
        {
            if(p_array[i]>maximum)
                maximum = p_array[i];
        }
        return maximum;
    }
    else
        return 0;
}

//通过下标访问数组
template<class Type>
Type CArray<Type>::operator [](const int index) const
{
    if(p_array)
    {
        if(index<m_Length)
            return p_array[index];
        else
            cerr<<"Index exceeds the maximum size!";
    }
    else
    {
        cerr<<"Empty array!";
    }
}
//判断两个数组是否相等
template<class Type>
bool CArray<Type>::operator ==(const CArray& cArr) const
{
    if(m_Length != cArr.m_Length)
    {
        return false;
    }
    else
    {
        for(int i=0;i<m_Length;i++)
        {
            if(p_array[i] != cArr.p_array[i])
            {
                return false;
            }
        }
        return true;
    }
}

//将一个数组赋给另一个数组
template<class Type>
bool CArray<Type>::operator =(const CArray<Type> & cArr) const
{
    if(m_Length != cArr.m_Length || p_array == NULL)
    {
        return false;
    }
    else
    {
        for(int i=0;i<m_Length;i++)
        {
            p_array[i] = cArr.p_array[i];
        }
        return true;
    }
}

#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岛上码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值