我写的向量

 

#include <iostream>
using namespace std;

class Vector
{
private:
 int *pHead;
 int *pCurrent_head;
 int *pCurrent_tail;
 int *pTail;
 int iCurrent_size;
 void Resize()
 {
  int *pDelete = pHead;
  int *pNew_current_head;
  int *pNew_current_tail;
  iCurrent_size *= 2;
  int *pNew_size = new int[iCurrent_size];
        pHead = pNew_size;
  pNew_current_head = pHead + 4;
  pNew_current_tail = pNew_current_head;
  while(pCurrent_head != pCurrent_tail)
  {
   *pNew_current_tail = *pCurrent_head;
   pCurrent_head++;
   pNew_current_tail++;
  }
  pCurrent_head = pNew_current_head;
  pCurrent_tail = pNew_current_tail;
  pTail = pHead + iCurrent_size;
  delete pDelete;
 }
public:
 Vector()
 {
  iCurrent_size = 8;
  pHead = new int[iCurrent_size];
  pCurrent_head = pHead + (iCurrent_size / 2);
  pCurrent_tail = pCurrent_head;
  pTail = pHead + iCurrent_size;
 }
 void Print()
 {
  int *pPrint = pCurrent_head;
  while(pPrint != pCurrent_tail)
  {
   cout<<*pPrint<<"  ";
    pPrint++;
  }
  cout<<endl;
 }
 void push_front(const int iValue)
 {
  if(pCurrent_head != pHead)
  {
   *pCurrent_head = iValue;
   --pCurrent_head;
  }
  else
  {
   Resize();
   *pCurrent_head = iValue;
   --pCurrent_head;
  }
 }
 void push_back(const int iValue)
 {
  if(pCurrent_tail != pTail)
  {
   *pCurrent_tail = iValue;
   pCurrent_tail++;
  }
  else
  {
   Resize();
   *pCurrent_tail = iValue;
            pCurrent_tail++;
  }
 }
 void pop_front()
 {
  if(pCurrent_head != pCurrent_tail)
  {
   pCurrent_head++;
  }
  else
  {
   cout<<"zhi xing le !"<<endl;
   exit(1);
  }
 }
 void pop_back()
 {
  if(pCurrent_head != pCurrent_tail)
  {
   pCurrent_tail--;
  }
  else
  {
   cout<<" zhe xing le !"<<endl;
   return;
  }
 }
 void Insert(int *pPosition, const int iValue)
 {
  if((pPosition < pCurrent_head) || (pPosition > pCurrent_tail))
  {
   return ;
  }
  else
  {
   if((pCurrent_head == pCurrent_tail) && (pPosition == pCurrent_head))
   {
    *pPosition = iValue;
    --pCurrent_head;
    ++pCurrent_tail;
   }
   else if( pCurrent_head > pHead)
   {
    int *pMove_tail = pCurrent_head;
    int *pMove_head = pMove_tail - 1;
    while(pMove_head != pPosition)
    {
     *pMove_head = *pMove_tail;
     pMove_head++;
     pMove_tail++;
    }
    *pMove_head = *pMove_tail;
    *pPosition = iValue;
   }
   else if(pCurrent_tail < pTail)
   {
    int *pMove_head = pCurrent_tail;
    int *pMove_tail = pMove_head + 1;
    while(pMove_head != pPosition)
    {
     *pMove_tail = *pMove_head;
     pMove_head--;
     pMove_tail--;
    }
    *pMove_tail = *pMove_head;
    *pPosition = iValue;
   }
   else
   {
    return ;
   }
  }
 }
 int size()const
 {
  int iSize = 0;
  int *pSize = pCurrent_head;
  while(pSize != pCurrent_tail)
  {
   iSize++;
   pSize++;
  }
  return iSize;
 }
 ~Vector()
 {
  delete pHead;
 }
};

int main()
{
 /*Vector vec;
 for(int i = 0; i < 15; i++)
  vec.push_back(i);
 int iSize = vec.size();
 cout<<iSize<<endl;
 cout<<"ge kai"<<endl;
 vec.Print();*/
   
 
 return 0;
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值