#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
class Myvector
{
private:
T *first;//首地址
T *last;//最后一个元素的下一地址
T *end;//容器尾地址
public:
//无参构造
Myvector():first(nullptr),last(nullptr),end(nullptr){}
//有参构造
Myvector(int num,const T &val )
{
first = new T[num];
last=first;
end=first+num-1;
for(int i=0;i<num;i++)
{
first[i]=val;
last++;
}
}
//拷贝构造
Myvector(const Myvector &other)
{
first = new T[other.end-other.first+1];
last=first;
end=first+(other.end-other.first);
for(int i=0;i<(other.last-other.first);i++)
{
first[i]=other.first[i];
last++;
}
}
//拷贝赋值
Myvector &operator=(const Myvector<T> *other) {
if (this != other)
{
delete[]first;
this->first = new T[other->end - other->first + 1];
this->last = other->last;
this->end = other->end;
for (int i = 0; i < other->end - other->first; ++i) {
this->first[i] = other->first[i];
}
}
return *this;
}
//析构
~Myvector()
{
delete [] first;
first=nullptr;
last=nullptr;
end=nullptr;
}
//at()
T &at(int pos)
{
if(pos<0||pos>(last-first-1))
{
cout<<"访问下标不合法"<<endl;
exit(0);
}
return first[pos];
}
//判空
bool empty()
{
return last==first;
}
//判满
bool full()
{
return first==end;
}
//front()
T &front()
{
return *first;
}
//back()
T &back()
{
return *(last-1);
}
//size()
int size()
{
return last-first;
}
int capacity()
{
return end-first+1;
}
//清空
void clear()
{
last=first;
}
//二倍扩容
void expand()
{
T *temp=new T[(this->end-this->first+1)*2];
this->last=temp;
for(int i=0;i<(this->end-this->first);i++)
{
temp[i]=this->first[i];
last++;
}
this->end=temp+(this->end-this->first+1);
delete []first;
this->first=temp;
temp=nullptr;
}
//push_back
void push_back(const T &val)
{
if(full())
expand();
*last=val;
last++;
}
//pop_back
void pop_back()
{
if(empty())
{
cout<<"容器已空"<<endl;
exit(0);
}
last--;
}
//begin()
const T *begin() const {
return first;
}
//end()
const T *pend() const {
return last;
}
//成员函数实现[]符号重载
const T &operator[](const int pos) const
{
return first[pos];
}
};
int main()
{
Myvector<int> v1(5,8);
Myvector<int> v2(v1);
for (int i = 0; i < v1.size(); i++)
{
cout << "v1[" << i << "] = " << v1[i] << endl;
}
cout<<"****************"<<endl;
for (int i = 0; i < v2.size(); i++)
{
cout << "v2[" << i << "] = " << v2[i] << endl;
}
cout<<"****************"<<endl;
//赋值构造函数
Myvector<int> v3;
v3 = v1;
for (int i = 0; i < v3.size(); i++)
{
cout << "v3[" << i << "] = " << v3[i] << endl;
}
cout << "**********************" << endl;
//清空v3
v3.clear();
cout << "v3.size() = " << v3.size() << endl;
cout << "*********************" << endl;
//判空
if (v3.empty())
cout << "v3容器已空!" << endl;
else
cout << "v3容器非空!" << endl;
cout << "************************" << endl;
//v3尾插
v3.push_back(1);
v3.push_back(2);
v3.push_back(3);
for (int i = 0; i < v3.size(); i++)
{
cout << "v3[" << i << "] = " << v3[i] << endl;
}
cout << "************************" << endl;
//第一个元素
cout << "v3.front = " << v3.front() << endl;
//最后一个元素
cout << "v3.back = " << v3.back() << endl;
cout << "************************" << endl;
//尾删
v3.pop_back();
for (int i = 0; i < v3.size(); i++)
{
cout << "v3[" << i << "] = " << v3[i] << endl;
}
return 0;
}
结果