#ifndef _MY_VECTOR_H
#define _MY_VECTOR_H
#include <string.h>
#include <assert.h>
template<class T>
class MyVector
{
public:
class iterator
{
public:
iterator():pelem(NULL){}
iterator(T *pt):pelem(pt){}
iterator(const iterator &iter);
iterator& operator = (const iterator &iter);
iterator& operator = (T *pt);
~iterator(){}
bool operator != (const iterator &iter);
iterator& operator ++ ();
iterator& operator ++ (int);
iterator& operator -- ();
iterator& operator -- (int);
iterator operator + (size_t size);
iterator operator - (size_t size);
iterator& operator -= (size_t size);
iterator& operator += (size_t size);
T& operator * ();
//functions add here
private:
T *pelem;
};
//constructor
MyVector():pbuff(NULL),beg(NULL),last(NULL),count(0),capcity(0){}
MyVector(const MyVector &orig);
MyVector& operator = (const MyVector &orig);
~MyVector();
//member function
T& operator [] (size_t index);
void pushback(const T &mt);
iterator insert(size_t index,const T &mt);
iterator insert(const T *phead,const T *pback, iterator p);
iterator erase(size_t index);
iterator erase(iterator phead, iterator pback);
void clear();
size_t size();
size_t capacity();
iterator begin();
iterator end();
private:
void del_buff()
{
if(NULL != pbuff)
{
delete pbuff;
pbuff = NULL;
}
}
T *pbuff;//Memory buff for elements
iterator beg;
iterator last;
size_t count;
size_t capcity;
};
/**MyVector's member functions**/
/**here are the member functions implementations**/
template<class T>
size_t MyVector<T>::size()
{
return count;
}
template<class T>
size_t MyVector<T>::capacity()
{
return capcity;
}
template<class T>
MyVector<T>::MyVector(const MyVector<T> &orig)
{
count = orig.size();
capcity = 2*count;
pbuff = new T [count*2];
size_t totalbytes = count*2*sizeof(T);
memcpy(pbuff,orig.pbuff,totalbytes);
}
template<class T>
MyVector<T>& MyVector<T>::operator = (const MyVector<T> &orig)
{
del_buff();
count = orig.size();
capcity = 2*count;
pbuff = new T [count*2];
size_t totalbytes = count*2*sizeof(T);
memcpy(pbuff,orig.pbuff,totalbytes);
return *this;
}
template<class T>
MyVector<T>::~MyVector<T>()
{
del_buff();
}
template<class T>
T& MyVector<T>::operator[](size_t index)
{
return pbuff[index];
}
template<class T>
void MyVector<T>::pushback(const T &mt)
{
if(NULL == pbuff && 0 == count)
{
pbuff = new T[(1+count)*2];
pbuff[0] = mt;
count++;
capcity = 2*count;
}
else
{
if(NULL != pbuff && count == capcity)
{
capcity *= 2;
T *ptem = new T[capcity];
size_t totalbytes = capcity*sizeof(T);
memcpy(ptem,pbuff,totalbytes);
del_buff();
pbuff = ptem;
pbuff[count] = mt;
count ++;
}
if(NULL != pbuff && count != capcity)
{
pbuff[count] = mt;
count++;
}
}
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::insert(size_t index,const T &mt)
{
assert(count >= index);
if(NULL != pbuff && count == capcity)
{
capcity *= 2;
T *ptem = new T[capcity];
memcpy(ptem,pbuff,capcity*sizeof(T));
ptem[index] = mt;
memcpy(&ptem[index+1],(count-index)*sizeof(T));
del_buff();
pbuff = ptem;
count ++;
typename MyVector<T>::iterator _iter(&pbuff[index]);
return _iter;
}
else if(NULL != pbuff && count != capcity)
{
size_t _end = count-1;
size_t _beg = index;
for(;_end >= _beg;_end--)
pbuff[_end+1] = pbuff[_end];
pbuff[index] = mt;
count++;
typename MyVector<T>::iterator _iter(&pbuff[index]);
return _iter;
}
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::
insert(const T *phead,const T *pback,
typename MyVector<T>::iterator p)
{
typename MyVector<T>::iterator _beg = begin(),_end = end();
size_t insertnum = 0;
for(;phead != pback;phead++)
insertnum++;
phead -= insertnum;
size_t index = 0;
for(;_beg != p;_beg++)
index++;
if(count +insertnum > capcity && NULL != pbuff)
{
capcity = 2*(count +insertnum);
T *ptem = new T [capcity];
memcpy(ptem,pbuff,(index)*sizeof(T));
memcpy(&ptem[index],phead,insertnum*sizeof(T));
memcpy(&ptem[index+insertnum],&pbuff[index],
(count-index)*sizeof(T));
del_buff();
pbuff = ptem;
count += insertnum;
typename MyVector<T>::iterator _iter(&pbuff[index]);
return _iter;
}
else if(count +insertnum <= capcity && NULL != pbuff)
{
for(size_t i = insertnum;i != 0;i--,count--)
pbuff[count+insertnum-1] = pbuff[count-1];
for(;phead != pback;phead++,p++)
*p = *phead;
count += insertnum;
return p;
}
if(NULL == pbuff && 0 == count)
{
capcity = 2*insertnum;
pbuff = new T[capcity];
memcpy(pbuff,phead,insertnum*sizeof(T));
count = insertnum;
typename MyVector<T>::iterator _iter(&pbuff[0]);
return _iter;
}
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::erase(size_t index)
{
T *temp = new T[count-index-1];
memcpy(temp,&pbuff[index+1],(count-index-1)*sizeof(T));
memcpy(&pbuff[index],temp,(count-index-1)*sizeof(T));
pbuff[count-1] = '\0';
count--;
delete [] temp;
typename MyVector<T>::iterator _iter(&pbuff[index]);
return _iter;
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::
erase( typename MyVector<T>::iterator phead,
typename MyVector<T>::iterator pback)
{
size_t elemnum = 0;
size_t _toend = 0;
for(;phead != pback;phead++)
elemnum++;
phead -= elemnum;
for(;pback != end();pback++)
_toend++;
pback -= _toend;
T *temp = new T[_toend];
memcpy(temp,&pbuff[count-_toend],_toend*sizeof(T));
memcpy(&pbuff[count-elemnum-_toend],temp,_toend*sizeof(T));
memset(&pbuff[count-elemnum],'\0',(count-elemnum)*sizeof(T));
delete [] temp;
count -= elemnum;
return phead;
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::begin()
{
beg = &pbuff[0];
return beg;
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::end()
{
last = &pbuff[count];
return last;
}
/**nested dependent typeclass**/
/**implementation**/
template<class T>
MyVector<T>::iterator::iterator(const typename MyVector<T>::iterator &iter)
{
pelem = iter.pelem;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::
operator = (const typename MyVector<T>::iterator &iter)
{
pelem = iter.pelem;
return *this;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::
operator = (T *pt)
{
pelem = pt;
return *this;
}
template<class T>
bool MyVector<T>::iterator::operator !=
(const typename MyVector<T>::iterator &iter)
{
if(pelem != iter.pelem)
return true;
else
return false;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ ()
{
++pelem;
return *this;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ (int)
{
pelem++;
return *this;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- (int)
{
pelem--;
return *this;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- ()
{
--pelem;
return *this;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::operator += (size_t size)
{
pelem += size;
return *this;
}
template<class T>
typename MyVector<T>::iterator& MyVector<T>::iterator::operator -= (size_t size)
{
pelem -= size;
return *this;
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::iterator::operator + (size_t size)
{
pelem += size;
typename MyVector<T>::iterator _iter(pelem);
return _iter;
}
template<class T>
typename MyVector<T>::iterator MyVector<T>::iterator::operator - (size_t size)
{
pelem -= size;
typename MyVector<T>::iterator _iter(pelem);
return _iter;
}
template<class T>
T& MyVector<T>::iterator::operator * ()
{
return *pelem;
}
#endif
Vector部分实现
最新推荐文章于 2022-02-17 19:20:54 发布