template<class T>
class DynamicArray
{
public:
T* ptrt;
int tcapacity;
int tsize;
public:
DynamicArray()
{
this->tcapacity = 5;
this->tsize = 0;
this->ptrt = new T[tcapacity];
}
~DynamicArray()
{
if (ptrt != NULL)
delete[] this->ptrt;
}
void InseetAtEnd(T t)//在尾部插入
{
if (this->tsize == this->tcapacity)
{
this->tcapacity += 5;
T* tmp;
tmp = new T[this->tcapacity];
int i;
for (i = 0;i < this->tsize;i++)
{
tmp[i] = this->ptrt[i];
}
delete[] this->ptrt;
this->ptrt = tmp;
}
this->ptrt[this->tsize] = t;
this->tsize++;
}
T& operator[](int index)//重载[]
{
return this->ptrt[index];
}
};
class Info
{
public:
int ID;
string name;
Info(int id,string str)
{
this->ID = id;
this->name = str;
}
Info() {};
};
void ForEach(DynamicArray<Info>& dynamicarr)
{
int i = 0;
for (;i < dynamicarr.tsize;i++)
{
cout << dynamicarr[i].name<<" "<<endl;
}
cout << endl;
}
void test()
{
DynamicArray<Info> dynamicarr;
Info I1(1, "aa"), I2(2, "bb"), I3(3, "cc"), I4(4, "dd"), I5(5, "ee"), I6(6, "ff");
dynamicarr.InseetAtEnd(I1);dynamicarr.InseetAtEnd(I2);dynamicarr.InseetAtEnd(I3);
dynamicarr.InseetAtEnd(I4);dynamicarr.InseetAtEnd(I5);dynamicarr.InseetAtEnd(I6);
ForEach(dynamicarr);
}
class DynamicArray
{
public:
T* ptrt;
int tcapacity;
int tsize;
public:
DynamicArray()
{
this->tcapacity = 5;
this->tsize = 0;
this->ptrt = new T[tcapacity];
}
~DynamicArray()
{
if (ptrt != NULL)
delete[] this->ptrt;
}
void InseetAtEnd(T t)//在尾部插入
{
if (this->tsize == this->tcapacity)
{
this->tcapacity += 5;
T* tmp;
tmp = new T[this->tcapacity];
int i;
for (i = 0;i < this->tsize;i++)
{
tmp[i] = this->ptrt[i];
}
delete[] this->ptrt;
this->ptrt = tmp;
}
this->ptrt[this->tsize] = t;
this->tsize++;
}
T& operator[](int index)//重载[]
{
return this->ptrt[index];
}
};
class Info
{
public:
int ID;
string name;
Info(int id,string str)
{
this->ID = id;
this->name = str;
}
Info() {};
};
void ForEach(DynamicArray<Info>& dynamicarr)
{
int i = 0;
for (;i < dynamicarr.tsize;i++)
{
cout << dynamicarr[i].name<<" "<<endl;
}
cout << endl;
}
void test()
{
DynamicArray<Info> dynamicarr;
Info I1(1, "aa"), I2(2, "bb"), I3(3, "cc"), I4(4, "dd"), I5(5, "ee"), I6(6, "ff");
dynamicarr.InseetAtEnd(I1);dynamicarr.InseetAtEnd(I2);dynamicarr.InseetAtEnd(I3);
dynamicarr.InseetAtEnd(I4);dynamicarr.InseetAtEnd(I5);dynamicarr.InseetAtEnd(I6);
ForEach(dynamicarr);
}