实现代码
#pragma once
#include
template
class Myvector
{
T *pBuff;
size_t num;
size_t maxNum;
public:
struct MyIterator
{
T *pIt;
MyIterator& operator=(MyIterator const& srcIt)
{
pIt = srcIt.pIt;
return *this;
}
MyIterator& operator+=(int n)
{
pIt = pIt + n;
return *this;
}
MyIterator& operator-=(int n)
{
pIt = pIt - n;
return *this;
}
T operator *()
{
return *pIt;
}
MyIterator operator+(int n)
{
MyIterator it;
it.pIt =pIt;
it.pIt += n;
return it;
}
MyIterator operator-(int n)
{
MyIterator it;
it.pIt = pIt;
it.pIt -= n;
return it;
}
MyIterator& operator++()
{
pIt += 1;
return *this;
}
MyIterator operator++(int)
{
MyIterator temp(*this);
pIt += 1;
return temp;
}
bool operator !=(MyIterator const& srcIt) const
{
return pIt != srcIt.pIt;
}
int operator -(MyIterator const& srcIt)const
{
return pIt - srcIt.pIt;
}
T& operator[](int index) const
{
return pBuff[index];
}
};
public:
MyIterator begin()
{
MyIterator it;
it.pIt = pBuff;
return it;
}
MyIterator end()
{
MyIterator it;
it.pIt = pBuff+num;
return it;
}
public:
Myvector();
Myvector(int n);
Myvector(Myvector const& other);
Myvector(int n,T const& srcDate);
Myvector(MyIterator b, MyIterator e);
~Myvector();
public:
size_t size() const;
size_t max_size() const;
size_t capacity() const;
void reserve(int n);
bool empty() const;
void clear();
public:
bool operator ==(Myvector const & srcVector) const;
bool operator !=(Myvector const & srcVector) const;
bool operator >=(Myvector const & srcVector) const;
bool operator <=(Myvector const & srcVector) const;
bool operator >(Myvector const & srcVector) const;
bool operator <(Myvector const & srcVector) const;
void operator =(Myvector const & srcVector);
//void operator =(T n);
T& operator [](int index) const;
public:
void swap(Myvector vector1);
public:
void assign(int n,T const& srcdate);//拷贝n个数据
void assign(MyIterator b,MyIterator e);//拷贝n个数据
public:
T at(int index)const;
public:
void push_back(T const & srcDate);//压入数据
void pop_back();//删除最后一个数据
void resize(size_t n);
public:
T front()const;
T back()const;
private:
//容器变大重分配
void Mymalloc()
{
if (num>maxNum)
{
maxNum = maxNum + ((maxNum >> 1)>1 ? (maxNum >> 1) : 1);
T* temppBuff = new T[maxNum];
memcpy(temppBuff, pBuff, sizeof(T)*num);
if (pBuff)
delete[]pBuff;
pBuff = temppBuff;
}
}
public:
MyIterator erase(MyIterator const &beg, MyIterator const &end)
{
int be = beg.pIt - pBuff, en = end.pIt - pBuff;
for (int i = be,j=en; i < num-en+be+1; i++,j++)
pBuff[i] = pBuff[j];
num =num-en+be;
MyIterator tempIt;
tempIt.pIt = pBuff + be;
return tempIt;
}
MyIterator erase(MyIterator const& pos)
{
int index = pos.pIt - pBuff;
for (int i = index; i < num; i++)
pBuff[i] = pBuff[i + 1];
num--;
MyIterator tempIt;
tempIt.pIt = pBuff+index;
return tempIt;
}
void insert(MyIterator const& pos, int n, T const& srcDate)
{
int index = pos.pIt - pBuff;
if (num + n > maxNum)
{
maxNum = maxNum + n;
T* temppBuff = new T[maxNum];
memcpy(temppBuff, pBuff, sizeof(T)*num);
if (pBuff)
delete[]pBuff;
pBuff = temppBuff;
}
printf("%d %d %d\n",index, num + n - 1, index + n);
for (int i = num + n - 1, j = num-1; i >= n + index - 1; i--, j--)
pBuff[i] = pBuff[j];
for (int i = index; i < index + n; i++)
pBuff[i] = srcDate;
num = num + n;
}
void insert(MyIterator const& pos, MyIterator const& beg, MyIterator const& end)
{
MyIterator temp = beg;
int index = pos.pIt - pBuff;
int n = end.pIt - beg.pIt;
if (num+n > maxNum)
{
maxNum = maxNum +n;
T* temppBuff = new T[maxNum];
memcpy(temppBuff, pBuff, sizeof(T)*num);
if (pBuff)
delete[]pBuff;
pBuff = temppBuff;
}
for (int i = num + n - 1, j = num - 1; i >= n + index - 1; i--, j--)
{
pBuff[i] = pBuff[j];
}
for (int i = index; i < index+n; i++)
{
pBuff[i] = *(temp);
temp = temp + 1;
}
num = num + n;
}
void insert(MyIterator const& pos, T const& srcDate)
{
int index = pos.pIt - pBuff;
if (num>=maxNum)
{
maxNum = maxNum + ((maxNum >> 1)>1 ? (maxNum >> 1) : 1);
T* temppBuff = new T[maxNum];
memcpy(temppBuff, pBuff, sizeof(T)*num);
if (pBuff)
delete[]pBuff;
pBuff = temppBuff;
}
for ( int i = num; i >index; i--)
{
pBuff[i] = pBuff[i - 1];
}
pBuff[index] = srcDate;
num++;
}
};
template
T Myvector::front()const
{
return pBuff[0];
}
template
T Myvector::back()const
{
return pBuff[num-1];
}
template
void Myvector::resize(size_t n)
{
if (n!=num)
{
int tempNum = n < num ? n : num;
T *temppBuff = new T[n];
memcpy(temppBuff, pBuff, sizeof(T)*tempNum);
if (pBuff)
delete[]pBuff;
pBuff = temppBuff;
num =maxNum= n;
}
}
template
void Myvector::clear()
{
memcpy(pBuff,0 , sizeof(T)*num);
num = 0;
}
template
void Myvector::pop_back(){
if (num)
num--;
}
template
void Myvector::push_back(T const & srcDate)
{
if (num>=maxNum)
{
maxNum = maxNum + ((maxNum >> 1)>1 ? (maxNum >> 1) : 1);
T* temppBuff = new T[maxNum];
memcpy(temppBuff, pBuff, sizeof(T)*num);
if (pBuff)
delete[]pBuff;
pBuff = temppBuff;
}
pBuff[num++] = srcDate;
}
template
T& Myvector::operator [](int index) const
{
return pBuff[index];
}
template
T Myvector::at(int index)const{
if (index < 0 || index >= num)
throw "数组越界";
return pBuff[index];
}
template
void Myvector::assign(MyIterator b, MyIterator e)
{
int n = e - b;
if (pBuff)
{
delete[]pBuff;
if (maxNum < n)
maxNum = n;
}
else
maxNum = n;
pBuff = new T[maxNum];
num = n;
for (int i = 0; i < num; i++)
pBuff[i] = *(b+i);
}
template
void Myvector::assign(int n, T const& srcdate)
{
if (pBuff)
{
delete[]pBuff;
if (maxNum < n)
max = n;
}
else
max = n;
pBuff = new T[maxNum];
num = n;
for (int i = 0; i < num; i++)
pBuff[i] = srcdate;
}
template
void Myvector::swap(Myvector swapVector)
{
size_t tempNum = num;
size_t tempMaxNum = maxNum;
T *temppBuff = pBuff;
num = swapVector.num;
maxNum = swapVector.maxNum;
pBuff = swapVector.pBuff;
swapVector.num=tempnum;
swapVector.maxNum = tempMaxNum;
swapVector.pBuff = temppBuff;
}
template
bool Myvector::operator !=(Myvector const & srcVector) const
{
return !(*this == srcVector);
}
template
void Myvector::operator =(Myvector const & srcVector)
{
num = srcVector.num;
maxNum = srcVector.maxNum;
if (num)
pBuff = srcVector.pBuff;
}
template
bool Myvector::operator >=(Myvector const & srcVector) const
{
size_t tempNum = num > srcVector.num ? num : srcVector.num;
for (size_t i = 0; i < tempNum; i++)
{
if (pBuff[i] < srcVector.pBuff[i])
return false;
}
return true;
}
template
bool Myvector::operator >(Myvector const & srcVector) const
{
size_t tempNum = num > srcVector.num ? num : srcVector.num;
for (size_t i = 0; i < tempNum; i++)
{
if (pBuff[i] <= srcVector.pBuff[i])
return false;
}
return true;
}
template
bool Myvector::operator <=(Myvector const & srcVector) const
{
size_t tempNum = num > srcVector.num ? num : srcVector.num;
for (size_t i = 0; i < tempNum; i++)
{
if (pBuff[i] > srcVector.pBuff[i])
return false;
}
return true;
}
template
bool Myvector::operator <(Myvector const & srcVector) const
{
size_t tempNum = num > srcVector.num ? num : srcVector.num;
for (size_t i = 0; i < tempNum; i++)
{
if (pBuff[i] >= srcVector.pBuff[i])
return false;
}
return true;
}
//template
//bool Myvector::operator >(Myvector const & srcVector) const
//{
// return !(*this <= srcVector);
//}
//template
//bool Myvector::operator <(Myvector const & srcVector) const
//{
// return !(*this >= srcVector);
//}
template
bool Myvector::operator ==(Myvector const & srcVector) const
{
if (num==servecor.num)
{
for (size_t i = 0; i < num; i++)
{
if (pBuff[i]!=srcVector.pBuff[i])
retrun false;
}
return true;
}
return false;
}
template
void Myvector::reserve(int n)
{
num = 0;
maxNum = n;
pBuff = new T[maxNum];
memset(pBuff,0,sizeof(T)*maxNum);
}
template
size_t Myvector::max_size() const
{
return INT_MAX / sizeof(T);
}
template
size_t Myvector::capacity() const
{
return maxNum;
}
template
size_t Myvector::size() const
{
return num;
}
template
bool Myvector::empty() const
{
return num == 0;
}
template
Myvector::Myvector(int n, T const& srcDate)
{
num = maxNum = n;
pBuff = NULL;
if (num)
{
pBuff = new T[num];
for (int i = 0; i < n; i++)
{
pBuff[i] = srcDate;
}
}
}
template
Myvector::~Myvector()
{
if (pBuff)
delete[]pBuff;
}
template
Myvector::Myvector(MyIterator b, MyIterator e)
{
int n=0;
if ((n=e-b)>0)
{
num = maxNum = n;
pBuff = new T[num];
for (int i = 0; i < num; i++)
pBuff[i] = *(b + i);
}
}
template
Myvector::Myvector(Myvector const& other)
{
num = other.num;
maxNum = other.maxNum;
if (num)
{
pBuff = new T[num];
memcpy(pBuff,other.pBuff,sizeof(T)*num);
}
}
template
Myvector::Myvector(int n)
{
num = n;
maxNum = n;
pBuff = new T[num];
memset(pBuff,0,sizeof(T)*num);
}
template
Myvector::Myvector()
{
pBuff = NULL;
num = 0;
maxNum = 0;
}