自己实现vector,对于自定义类型可用,可以实现vector的嵌套。功能上目前只实现了插入和删除。

今天是元宵节,人家是在房里啪啪啪,我也在房里啪啪啪,不过人家在chuang上,我却是在电脑前。相当无聊透顶,仿照STL中的vector写了一个自己的Vector,目前只实现了插入和删除的操作,实现的功能不多。目前写的这个的最大优点在于实现了对自定义类型的通用性。缺点是中间变量优点多。   请各位看官笑纳,不足之处请补充,若有内存泄露什么的一定要指出来,废话不多说,上代码,多组。转载请注明出处

#include <iostream>
#include <assert.h>
#include <malloc.h>
using namespace std;
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利


const int SIZE = 10;
const int INCREASE = 5;


template <class Type>
class Vector
{
Type *Phead;
size_t CurSize;
size_t MaxSize;
private:
size_t Maxsize()
{
return MaxSize;
}
void Increase()
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
Type *New = (Type*)malloc(sizeof(Type)*(Size()+INCREASE));
assert(New != NULL);
size_t i;
for(i = 0;i < Size();++i)
{
New[i] = Phead[i];
}
MaxSize+=INCREASE;
free(Phead);
Phead = New;
}
void Swap(Vector<Type> &Coll)
{
Type *tmp = Phead;
Phead = Coll.Phead;
Coll.Phead = tmp;
}
public:
Vector<Type>()
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
Phead = (Type*)malloc(sizeof(Type)*SIZE);
assert(Phead != NULL);
CurSize = 0;
MaxSize = SIZE;
}
Vector<Type>(Vector<Type> &Coll)
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
Phead = (Type*)malloc(sizeof(Type)*Coll.Size());
assert(Phead != NULL);
size_t i;
for(i = 0;i < Coll.Size();++i)
{
Phead[i] = Coll.Phead[i];
}
this->CurSize = Coll.Size();
this->MaxSize = Coll.Size();
}
Type& operator[] (size_t pos)
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
assert(pos < Size());
return Phead[pos];
}
Vector<Type>& operator=(Vector<Type> &Coll)
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
if(Phead != NULL)
{
free(Phead);
}
Phead = (Type*)malloc(sizeof(Type)*Coll.Size());
assert(Phead != NULL);
size_t i;
for(i = 0;i < Coll.Size();++i)
{
Phead[i] = Coll[i];
}
this->CurSize = Coll.Size();
this->MaxSize = Coll.Size();
return *this;
}
size_t Size()
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
return CurSize;
}
bool IsEmpty()
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
return Size() == 0;
}
bool IsFull()
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
return Size() == Maxsize();
}
void InsertPos(size_t pos,Type tmp)
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
assert(pos <= CurSize);
if(IsFull())
{
this->Increase();
}
size_t i;
for(i = Size();i > pos;--i)
{
new(Phead+i) Vector<Type>();
Phead[i] = Phead[i-1];
}
new(Phead+pos) Vector<Type>();
Phead[pos] = tmp;
++CurSize;
}
void Erase(size_t pos)
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
if(pos >= Size())
{
return ;
}
else
{
size_t i;
for(i = pos;i < Size()-1;++i)
{
Phead[i] = Phead[i+1];
}
--CurSize;
}
}
void Push_Back(Type tmp)
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
this->InsertPos(this->Size(),tmp);
}
void Push_Front(Type tmp)
{
this->InsertPos(0,tmp);
}
void Pop_Back()
{
this->Erase(Size()-1);
}
void Pop_Front()
{
this->Erase(0);
}
void Clear()
{
CurSize = 0;
}
~Vector()
{
free(Phead);
}
void Show()
{
//转载请注明出处http://my.csdn.net/u010632868,CSDN 第25个比利
size_t i;
for(i = 0;i < Size();++i)
{
cout<<Phead[i]<<" ";
}
cout<<endl;
}
};


struct Int
{
int Date;
Int(int x = 0) : Date(x) {}
Int(Int &x)
{
Date = x.Date;
}
};


ostream& operator<<(ostream &os,Int &x)
{
os<<x.Date;
return os;
}




int main()
{
Vector<Vector<Int>> Coll;
Vector<Int> coll;

int i;
for(i = 0; i <3;++i)
{
Int a(1);
coll.Push_Back(a);
}
Coll.Push_Back(coll);
coll.Clear();
for(i = 0;i < 6;++i)
{
Int a(3);
coll.Push_Back(a);
}
Coll.Push_Back(coll);
size_t k;
for(k = 0;k < Coll.Size();++k)
{
Coll[k].Show();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值