简单实现vector
void main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
}
//vector.h
#ifndef _VECTOR_H
#define _VECTOR_H
#include"memory.h"
template<class _Ty,class _A = _my_allocator<_Ty> >
class vector
{
public:
typename typedef _A::size_type size_type;
typename typedef _A::value_type value_type;
typename typedef _A::pointer pointer;
typename typedef _A::const_pointer const_pointer;
typename typedef _A::reference reference;
typename typedef _A::const_reference const_reference;
typename typedef _A::pointer iterator;
typename typedef _A::const_pointer const_iterator;
public:
explicit vector(const _A& _A1 = _A()):allocator(_A1),_First(0),_Last(0),_End(0)
{}
public:
iterator begin()
{return _First;}
iterator end()
{return _Last;}
public:
void push_back(const _Ty& _X)
{insert(end(),_X);}
size_type size()const
{return (_First == 0?0:_Last-_First);}
public:
iterator insert(iterator _P,const _Ty& _X = _Ty())
{
size_type _O = _P - begin();
insert(_P,1,_X);
return _O+begin();
}
void insert(iterator _P,size_type _M,const _Ty& _X)
{
if(_End - _Last < _M)//容量不够
{
size_type _N = size() + (size()>_M?size():_M);//至少开辟为原来的2倍
iterator _S = allocator.allocator(_N,(void*)0);//申请空间
iterator _Q = _Ucopy(_First,_P,_S);//从_First到_P区间的拷入到_S的位置(初始数据)
_Ufill(_Q,_M,_X);//将_M个_X填充到_Q位置
_Ucopy(_P,_Last,_Q+_M);//从P到Last区间内数据拷入到Q+M的位置上
_Destory(_First,_Last);//摧毁原有的空间,调用析构函数(释放该对象所指的空间)
allocator.deallocator(_First,_End-_First);//delete该对象,释放空间
_Last = _S+size()+_M;
_End = _S+_N;
_First = _S;//???????????????????????????????????
}
else if(_Last - _P < _M)//容量够,空间不足
{
_Ucopy(_P,_Last,_P+_M);
_Ufill(_Last,_M-(_Last-_P),_X);//先将Last所指空间数据填充进去,(因为Last所指的是最后一个元素的下一个)
fill(_P,_Last,_X);//再填充P到Last区域间的数据
_Last += _M;
}
else if(0<_M)
{
///
_Ucopy(_Last - _M, _Last, _Last);
copy_backward(_P, _Last - _M, _Last);
fill(_P, _Last, _X);
_Last += _M;
}
}
protected:
void _Destory(iterator _F,iterator _L)
{
for(;_F!=_L;++_F)
allocator.destory(_F);
}
iterator _Ucopy(const_iterator _F,const_iterator _L,iterator _P)//未初始化拷贝,在P所指位置拷贝一个从F到L的区间
{
for(;_F!=_L;++_F,++_P)
allocator.construct(_P,*_F);
return _P;
}
void _Ufill(iterator _F,size_type _N,const _Ty& _X)//在F所指位置填充N个X
{
for(;_N>0;--_N,++_F)
allocator.construct(_F,_X);
}
void _Xran()const//异常函数,抛出异常
{
_THROW(out_of_range,"invalid vector<T> subscript!");//发生越界
}
private:
_A allocator;
iterator _First;
iterator _Last;
iterator _End;
};
#endif
//memory.h
#ifndef _MEMORY_H
#define _MEMORY_H
template<class _Ty>
_Ty* _My_Allocator(ptrdiff_t _N,_Ty*)
{
if(_N<0)
_N = 0;
return((_Ty*)operator new((size_t)_N*sizeof(_Ty)));
}
template<class _T1,class _T2>
void _My_Construct(_T1* _P,const _T2& _V)
{
new((void*)_P)_T1(_V);//placement new (定位new)_P所指的位置构造一个_T1对象用_V的值初始化
}
template<class _Ty>
void _My_Destory(_Ty* _P)
{
_P->~_Ty();
}
template<class _Ty>
class _my_allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Ty* pointer;
typedef const _Ty* const_pointer;
typedef _Ty & reference;
typedef const _Ty & const_reference;
typedef _Ty value_type;
public:
_Ty* address(_Ty& _X)const
{return(&_X);}
const _Ty* address(const _Ty& _X)const
{return(&_X);}
_Ty* allocator(size_t _N,const void *)//申请空间
{
return(_My_Allocator((ptrdiff_t)_N,(_Ty*)0));
}
char* _Charalloc(size_t _N)//申请字符空间
{
return(_My_Allocator((ptrdiff_t)_N,(char*)0));
}
void deallocator(void* _P,size_t)//释放空间
{
operator delete(_P);
}
void construct(_Ty* _P,const _Ty& _V)
{
_My_Construct(_P,_V);
}
void destory(_Ty* _P)
{
_My_Destory(_P);
}
size_t max_size()
{
size_t _N = (size_t)(-1)/sizeof(_Ty);
return (0<_N?_N:1);
}
};
#endif
STL(vector)
最新推荐文章于 2022-11-02 20:45:00 发布