作业:手动实现My_vector
代码如下:
#include <iostream>
using namespace std;
template<typename T>
class Myvector
{
private:
T *front;
T *last;
T *end;
public:
//无参构造
Myvector();
//有参构造
Myvector(T d,int len);
//拷贝构造
Myvector(const Myvector &r);
//拷贝赋值
Myvector<T> &operator=(const Myvector &other);
//析构函数
~Myvector();
//at函数
T at(int a);
//empty函数
bool empty();
//full函数
bool full();
//tfront函数
T tfront();
//back函数
T &back();
//size函数
int size();
//push_back函数
void push_back(T data);
//pop_back函数
void pop_back();
//2倍扩容函数
void expand();
//展示函数
void show();
};
template <typename T>
Myvector<T>::Myvector()
{
front = last = end = nullptr;
cout<<"无参构造"<<endl;
}
template <typename T>
//有参构造
Myvector<T>::Myvector(T d,int len)
{
front = new T[len];
for(int i=0;i<len;i++)
{
*(front+i) = d;
}
last = end = front+len;
cout<<"有参构造"<<endl;
}
template <typename T>
//拷贝构造
Myvector<T>::Myvector(const Myvector &r)
{
front = new T[r.end-r.front];
memcpy(front,r.front,sizeof((r.end-r.front)*sizeof(T)));
last = front+(r.last-r.front);
end = front+(r.end-r.front);
cout<<"拷贝构造"<<endl;
}
template <typename T>
//拷贝赋值
Myvector<T> & Myvector<T>::operator=(const Myvector &other)
{
if((end-front)<(other.end-other.front))
{
delete front;
front = nullptr;
front = new T[other.end-other.front];
end = front+(other.end-other.front);
}
memcpy(front,other.front,((other.end-other.front)*sizeof(T)));
last = front+(other.last-other.front);
cout<<"拷贝赋值"<<endl;
return *this;
}
template <typename T>
//析构函数
Myvector<T>::Myvector::~Myvector()
{
delete []front;
front = nullptr;
cout<<"析构函数"<<endl;
}
template <typename T>
//at函数
T Myvector<T>::Myvector::at(int a)
{
if(a<0 || a>(last-front-1))
{
cout<<"所给数据不合理"<<endl;
}
return *(front+a);
}
template <typename T>
//empty函数
bool Myvector<T>::Myvector::empty()
{
if(front == last)
{
return true;
}
else
{
return false;
}
}
template <typename T>
//full函数
bool Myvector<T>::full()
{
if(last == end)
{
return true;
}
else
{
return false;
}
}
template <typename T>
//tfront函数
T Myvector<T>::tfront()
{
if(empty())
{
cout<<"数据为空"<<endl;
}
return *front;
}
template <typename T>
//back函数
T &Myvector<T>::back()
{
if(empty())
{
cout<<"数据为空"<<endl;
}
return *(last-1);
}
template <typename T>
//size函数
int Myvector<T>::size()
{
return last-front;
}
template <typename T>
//push_back函数
void Myvector<T>::Myvector::push_back(T data)
{
if(last != end)
{
*last = data;
last++;
cout<<"push1"<<endl;
}
else if(last == end && !empty())
{
expand();
cout<<"二倍扩容"<<endl;
*last = data;
last++;
cout<<"push"<<endl;
}
else if(empty())
{
front = new T;
*front = data;
last++;
end = last;
cout<<"push2"<<endl;
}
cout<<"push3"<<endl;
}
template <typename T>
//pop_back函数
void Myvector<T>::pop_back()
{
if(empty())
{
cout<<"数据为空"<<endl;
return;
}
last--;
}
template <typename T>
//2倍扩容函数
void Myvector<T>::expand()
{
int *temp;
temp = new T[(end-front)*2];
memcpy(temp,front,((end-front)*sizeof(T)));
last = temp+(end-front);
end = temp+((end-front)*2);
delete front;
front = nullptr;
front = temp;
cout<<"二倍扩容"<<endl;
}
template <typename T>
//展示函数
void Myvector<T>::show()
{
for(int i=0;(front+i)!=last;i++)
{
cout<<*(front+i);
}
cout<<endl;
}
int main()
{
Myvector<int> m1(5,5);
m1.show();
m1.push_back(7);
m1.show();
m1.push_back(7);
m1.push_back(7);
m1.push_back(7);
m1.push_back(7);
m1.push_back(7);
m1.push_back(7);
m1.push_back(7);
m1.show();
return 0;
}