#include <iostream>
#include <cstring>
#define Max 10
using namespace std;
template <typename T>
class Vectors
{
public:
T *first;
T *last;
T *end;
//有参构造
Vectors():first(new T(NULL)),last(first-1),end(first-1){}
//有参构造
Vectors(int num,const T &val)
{
first =new T[num];
last =first+num-1;
end=first+num-1;
for(int i=0;i<num;i++)
{
first[i]=val;
}
}
//拷贝构造
Vectors(const Vectors &from)
{
int len=sizeof(from)/sizeof(T);
first=new T[len];
last =first+len-1;
end=first+len-1;
for(int i=0;i<len;i++)
{
first[i]=from[i];
}
}
//析构函数
~Vectors()
{
delete []first;
}
//从一个容器的起始地址到终止地址的元素,来初始化一个vector
Vectors ( T* other_start, T* other_end )
{
first=new T[other_end-other_start];
end=last=first+(other_end-other_start)-1;
for(int i=0;i<other_end-other_start;i++)
{
first[i]=other_start[i];
}
}
int size()
{
return last-first+1;
}
int capacity()
{
return end-first+1;
}
//添加值为val的元素到当前vector末尾
void push_back(const T& val)
{
if(size()<capacity())
{
*(++last)=val;
}
else
{
if(capacity()==0)
{
T *temp=new T[1];
delete []first;
first=temp;
first[0]=val;
last=end=temp;
}
else
{
T *temp=new T[2*capacity()];
memcpy(temp,first,capacity()*sizeof(T));
int n=capacity();
delete []first;
first=temp;
first[n]=val;
last=temp+n;
end=temp+2*n-1;
}
}
}
//empty()如果当前vector没有容纳任何元素,则empty()函数返回true,否则返回false
bool empty()
{
return first==last+1;
}
//函数删除当前vector最末的一个元素
void pop_back()
{
*last=0;
last--;
}
//at() 函数 返回当前Vector指定位置loc的元素的引用. at() 函数 比 [] 运算符更加安全, 因为它不会让你去访问到Vector内越界的元素
T& at(const int loc)
{
if(empty())
{
cout<<"Vector is empty"<<endl;
exit(0);
}
else if(loc>capacity()-1)
{
cout<<"期望访问不大于"<<capacity()-1<<"的元素"<<endl;
}
else
{
return first[loc];
}
}
//front()函数返回当前vector起始元素的引用
T &front()
{
if(empty())
{
cout<<"Vector is empty"<<endl;
exit(0);
}
else
{
return *first;
}
}
//函数返回当前vector最末一个元素的引用
T &back()
{
if(empty())
{
cout<<"Vector is empty"<<endl;
exit(0);
}
else
{
return *last;
}
}
//begin()函数返回一个指向当前vector起始元素的迭代器
T* begin()
{
if(empty())
{
cout<<"Vector is empty"<<endl;
exit(0);
}
else
{
return first;
}
}
//end() 函数返回一个指向当前vector末尾元素的下一位置的迭代器.注意,如果你要访问末尾元素,需要先将此迭代器自减1
T* End()
{
if(empty())
{
cout<<"Vector is empty"<<endl;
exit(0);
}
else
{
return last+1;
}
}
//clear()函数删除当前vector中的所有元素
void clear()
{
memset(first,0,capacity());
last=first-1;
}
};
int main()
{
Vectors<int> v1; //定义一个容器
cout<<"v1.size = "<<v1.size()<<endl;
cout<<"v1.capacity = "<<v1.capacity()<<endl;
cout<<"************************************************************\n";
for(int i=1; i<=20; i++)
{
v1.push_back(i);
cout<<"v1.size = "<<v1.size()<<" v1.capacity = "<<v1.capacity()<<endl;
}
//输出当前vector中的内容
for(int i=0; i<v1.size(); i++)
{
//cout<<v1[i]<<" ";
cout<<v1.at(i)<<" ";
}
cout<<endl;
//调用尾删函数
v1.pop_back();
for(int i=0; i<v1.size(); i++)
{
//cout<<v1[i]<<" ";
cout<<v1.at(i)<<" ";
}
cout<<endl;
//将第一个元素的值改为100,最后一个值改为200
v1.front() = 100;
v1.back() = 200;
for(int i=0; i<v1.size(); i++)
{
//cout<<v1[i]<<" ";
cout<<v1.at(i)<<" ";
}
cout<<endl;
//使用迭代器来遍历数组
for(auto p=v1.begin(); p!=v1.End(); p++)
{
cout<<*p<<" ";
}
cout<<endl;
//清空vector
v1.clear();
cout<<"v1.size = "<<v1.size()<<endl;
cout<<"v1.capacity = "<<v1.capacity()<<endl;
// for(int i=0; i<v1.capacity(); i++)
// {
// //cout<<v1[i]<<" ";
// cout<<v1.at(i)<<" ";
// }
// cout<<endl;
//使用有参构造函数构造一个vector
Vectors <char> v2(5, 'A');
for(int i=0; i<v2.size(); i++)
{
//cout<<v2[i]<<" ";
cout<<v2.at(i)<<" ";
}
cout<<"v2.size = "<<v2.size()<<endl; //5
cout<<"v2.capacity = "<<v2.capacity()<<endl; //5
v2.push_back('G');
cout<<"v2.capacity = "<<v2.capacity()<<endl; //10
//使用别的容器给新容器初始化
int arr[] = {3,8,4,6,2,9,1,0,7};
Vectors<int> v3(arr, arr+5);
for(int i=0; i<v3.size(); i++)
{
// cout<<v3[i]<<" ";
cout<<v3.at(i)<<" ";
}
}
手动实现vector模板类
最新推荐文章于 2024-08-21 18:16:36 发布