数组类模板的封装

#include<iostream>
using namespace std;

template <typename T1>
class Myarray
{
private:
    T1 *a_ptr;
    int a_size;
    int a_len;

pbulic:
    Myarray();//无参构造
    Myarray(const Myarray<T1> &r);//深拷贝构造
    ~Myarray();
    void show();//打印数组
    int full();//判满
    int empty();//判空
    void insert (int pos,T1 data);//按照位置插入数据
    void chenge (int pos,T1 data);//按照位置修改数据
    void delete (int pos);//按照位置删除
};
template <typename T1>
void delete (int pos)//按照位置删除
{
    if(0 == this->empty())
    {
        cout << "the array is empty" << endl;
        return NULL;
    }
    if(pos<0 || pos>=a_len)
    {
        cout << "pos is defoult" << endl;
        return NULL;
    }
    for(int i=pos;i<a_len;i++)
    {
        a_ptr[i]=a_ptr[i+1];
    }
    a_len -= 1;
}
template <typename T1>
void show()//打印数组
{
    if(0 == this->empty())
    {
        cout << "the array is empty" << endl;
        return NULL:
    }
    for(int i=0;i<this->a_len;i++)
    {
        cout << a_ptr[i] << " ";
    }
    cout<<endl;
}
template <typename T1>
int full()//判满
{
    if(this->a_len == this->a_size)
    {
        return 1;//1为满
    }
    return 0;
}

template <typename T1>
int empty()//判空
{
    if(this->a_len == 0)
    {
        return 0;//0为空
    }
    return -1;
}
template <typename T1>
void insert (int pos,T1 data)//按照位置插入数据
{
    if(1==this->empty())//判空
    {
        cout << "the array is empty" << endl;
        return NULL;
    }
    if(pos<0 || pos>a_len)//判断位置是否正确
    {
        cout << "pos is defoult(0<pos<=a_len)" << endl;
        return NULL;
    }
    for(int i=a_len;i>=pos;i--)
    {
        a_ptr[i]=a_ptr[i+1];
    }
    a_ptr[pos]=data;
    a_len += 1;
}

void chenge (int pos,T1 data)//按照位置修改数据
{
    if(0 == this->empty())
    {
        cout << "the array is empty" << endl;
        return NULL;
    }
    if(pos<0 || pos>=a_len)
    {
        cout << "the pos is defoult" << endl;
        return NULL;
    }
    a_ptr[pos]=data;
}

template <typename T1>
Myarray<T1>::Myarray()
{
    a_ptr = new T1[50];
    a_size=50;
    a_len = 0;
}

template <typename T1>
Myarray<T1>::Myarray(const Myarryay<T1> & r)
{
    this->a_ptr=new T1[*(r.a_ptrr)];
    for(int i=0;i<r.a_size;i++)
    {
        this->a_ptr[i]=r.a_ptr[i];
    }
    this->a_size=r.a_size;
    this->a_len=r.a_len;
}

template <typename T1>
Myarray<T1>::~Myarray()
{
    if(a_ptr != NULL)
    {    
        delete [] a_ptr;
        a_ptr=NULL;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值