#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;
}
}
数组类模板的封装
最新推荐文章于 2023-02-15 17:31:22 发布