自己写vector

   昨天看《程序员面试宝典》一道题目问Vector怎么实现的,结果给的答案是怎么使用,想起好久没写C++了,于是兴起写了一个,折腾到晚上2点,还是有很多Bug,难点在于内存分配方面。。

/*================================================================================

文件名: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;
}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值