模仿stl实现自己的vector

  1. // Iterator.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. template <class T>
  7. class Iterator
  8. {
  9. public:
  10.     Iterator()
  11.     {
  12.         this->m_pData = NULL;
  13.         this->m_nPos = 0;
  14.         this->m_nSize = 0;
  15.         this->m_nLen = 0;
  16.     }
  17.     ~Iterator()
  18.     {
  19.         if (NULL != this->m_pData)
  20.         {
  21.             delete[] this->m_pData;
  22.             this->m_pData = NULL;
  23.         }
  24.     }
  25. public:
  26.     virtual T* end() = 0;
  27.     virtual T* begin() = 0;
  28. protected:
  29.     int m_nPos;
  30.     int m_nSize;
  31.     int m_nLen;
  32.     T*  m_pData;
  33. };
  34. template <class T>
  35. class CMyVector:public Iterator<T>
  36. {
  37. public:
  38.     typedef T* iterator;
  39. public:
  40.     CMyVector(int nSize = 10)
  41.     {
  42.         if (nSize < 0)
  43.         {
  44.             nSize = 10;
  45.         }
  46.         this->m_pData = new T[this->m_nSize];
  47.     }
  48.     ~CMyVector()
  49.     {
  50.     }
  51. public:
  52.     int Length()
  53.     {
  54.         return this->m_nLen;
  55.     }
  56.     bool Push(T obj)
  57.     {
  58.         if (this->m_nLen >= this->m_nSize)
  59.         {
  60.             //get more memory
  61.             int nSize = this->m_nSize * 2 + 1;
  62.             T* pTemp = new T[nSize];
  63.             if (NULL == pTemp)
  64.             {
  65.                 return false;
  66.             }
  67.             this->m_nSize = nSize;
  68.             ::memset(pTemp, 0, sizeof(T) * this->m_nSize);
  69.             ::memcpy(pTemp, this->m_pData, sizeof(T)*this->m_nLen);         
  70.             delete[] this->m_pData;
  71.             this->m_pData = pTemp;
  72.         }
  73.         ::memcpy(this->m_pData + this->m_nLen, &obj, sizeof(obj));
  74.         this->m_nLen ++;
  75.         return true;
  76.     }
  77. public:
  78.     virtual T* end()
  79.     {
  80.         return this->m_pData + this->m_nLen;
  81.     }
  82.     virtual T* begin()
  83.     {
  84.         this->m_nPos = 0;
  85.         return this->m_pData;
  86.     }
  87. };
  88. int _tmain(int argc, _TCHAR* argv[])
  89. {
  90.     int i = 0;
  91.     CMyVector<int> vtData;
  92.     CMyVector<int>::iterator it;
  93.     for (i = 0; i < 10; i++)
  94.     {
  95.         vtData.Push(i);
  96.         cout << "nLen = " << vtData.Length() << endl;
  97.     }
  98.     cout << "vector data: " << endl;
  99.     for (it = vtData.begin(); it != vtData.end(); it++)
  100.     {
  101.         cout << *it << "/t";
  102.     }
  103.     cout << endl;
  104.     return 0;
  105. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值