#include <iostream>
using namespace std;
template <typename T>
class my_vct
{
private:
T*first;
T*last;
T*end;
public:
my_vct(){}
my_vct(int size)
{
first=new T[size];
last=first;
end=first+size;
}
~my_vct()
{
delete []first;
first=nullptr;
last=nullptr;
end=nullptr;
}
my_vct(const my_vct &other)
{
int len=other.last-other.first;
int size=other.end-other.first;
this->first=new T[size];
memcpy(this->first,other.first,len*sizeof (T));
this->last=this->first+len;
this->end=this->first+size;
}
bool my_empty()
{
return this->first==this->last;
}
bool my_full()
{
return this->first==this->end;
}
void my_greater()
{
int size=this->end-this->first;
T *temp=new T[2*size];
memcpy(temp,this->first,size*sizeof(T));
delete []first;
first=temp;
last=first+size;
end=first+2*size;
}
void my_pushback(const T val)
{
if(this->my_full())
this->my_greater();
*last=val;
last++;
}
void my_popback()
{
if(this->my_empty())
return;
--last;
}
T&front()
{
return *first;
}
int size()
{
return end-first;
}
int len()
{
return last-first;
}
T&at(int pos)
{
if(pos<0||pos>this->len())
cout<<"访问越界,不合法 "<<endl;
return first[pos];
}
};
int main()
{
my_vct< int > my(10);
for(int i=0; i<20; i++)
{
my.my_pushback(i);
cout<<my.size()<<endl;
}
for(int i=0;i<20;i++)
{
cout<<my.at(i)<<endl;
}
return 0;
}
0401-0403作业
最新推荐文章于 2024-08-07 10:18:30 发布