/*================================================================================
文件名:MyVector.h
功能:模拟STL中的vector容器
作者:sunnyrain
日期:2007-10-20
编译环境:MinGW Studio
=================================================================================*/
#include<cstddef>
#include<cassert>
using namespace std;
template<class T>
class MyVector
{
public:
typedef T* Iterator;
private:
T* mem;
size_t pSize; //构建时欲分配内存大小,容器容量
size_t nSize; //实际元素个数
Iterator pBegin; //指向向量第一个元素
Iterator pEnd; //指向向量最后一个元素的下一位
public:
MyVector(Iterator b,Iterator e); //根据数组构建vector
MyVector(size_t s); //根据大小构建容器,并初始化为0
MyVector(); //默认构造函数
~MyVector();
Iterator begin(); //返回第一个元素地址
Iterator end(); //返回最后一个元素下一个地址
size_t size(); //返回向量长度
void reserve(size_t t);
void push_back(T t); //添加一个元素到向量末尾
size_t compacity();
T & operator [](size_t i); //返回向量中第i个元素
};
template<class T> //根据数组创建容器
MyVector<T>::MyVector(Iterator b,Iterator e):pSize(1024)
{
this->nSize = size_t(e - b);
this->mem = (T*)new char[nSize*sizeof(T)];
this->pBegin = this->mem;
this->pEnd = this->mem + this->nSize;
for(size_t i=0;i< this->nSize; ++i)
{
new (mem+i) T(*(b+i));
}
}
template<class T>
MyVector<T>::MyVector(size_t s):pSize(1024) //根据大小构建容器
{
if(s == 0)
{
this->mem = NULL;
this->pBegin = NULL;
this->pEnd = NULL;
this->nSize = 0;
}
else
{
while(pSize < s)
pSize *= 2;
this->nSize = s;
this->mem = (T*)new char[pSize*sizeof(T)];
new (this->mem) T[s];
this->pBegin = this->mem;
this->pEnd = this->mem + s;
}
}
template<class T>
MyVector<T>::MyVector():pSize(1024) //默认构造函数
{
this->nSize = 0;
this->mem = (T*)new char[this->pSize*sizeof(T)];
this->pBegin = this->pEnd = mem;
}
template<class T> //默认析构函数
MyVector<T>::~MyVector()
{
for(size_t i = this->nSize;i > 0;i--)
this->mem[i-1].~T();
delete[] (char*)this->mem;
}
template<class T> //返回容器起始元素位置
typename MyVector<T>::Iterator MyVector<T>::begin()
{
return this->pBegin;
}
template<class T> //返回末元素后的位置
typename MyVector<T>::Iterator MyVector<T>::end()
{
return this->pEnd;
}
template<class T> //返回容器中元素个数
size_t MyVector<T>::size()
{
return this->nSize;
}
template<class T> //预留t大小的内存,包括当前已有元素数目
void MyVector<T>::reserve(size_t t)
{
if(t < this->nSize)
{
exit(1);
}
T *p = (T*)new char[t*sizeof(T)];
if(this->nSize > 0)
memcpy(p,this->mem,this->nSize*sizeof(T));
delete[] (char*)this->mem;
this->mem = p;
this->pBegin = p;
this->pEnd = p + this->nSize;
}
template<class T> //向容器中添加一个T对象
void MyVector<T>::push_back(T t)
{
if(nSize == pSize)
{
pSize *= 2;
T* p = (T*)new char[pSize*sizeof(T)];
memcpy(p,this->mem,nSize*sizeof(T));
delete[] (char*)this->mem;
this->mem = p;
this->pBegin = p;
this->pEnd = this->mem + nSize;
}
new (this->mem + this->nSize) T(t);
++pEnd;
++nSize;
}
template<class T> //返回容器实际已经申请到的内存容量
size_t MyVector<T>::compacity()
{
return this->pSize;
}
template<class T> //[]操作符重载
T& MyVector<T>::operator [](size_t i)
{
if(i<0 || i>= this->nSize)
{
exit(1);
}
return mem[i];
}
/*================================================================================
文件名:main.cpp
功能:测试MyVector
作者:sunnyrain
日期:2007-10-20
编译环境:MinGW Studio
=================================================================================*/
#include<iostream>
#include"MyVector.h"
using namespace std;
class A{
public:
static int count;
A()
{
++count;
cout<<count<<"th A object constructed!"<<endl;
}
~A()
{
cout<<count<<"th A object deconstructed!"<<endl;
--count;
}
};
int A::count = 0;
int main()
{
MyVector<int> a(10);
cout<<a.size()<<endl;
a.push_back(11);
cout<<a.size()<<endl;
//a.reserve(2048)
for(size_t i=0;i<a.size();i++)
{
cout<<a[i]<<endl;
}
MyVector<A> b(5);
return 0;
}