C++ Vector的简单实现

[b]vector.h的代码[/b]

#ifndef VECTOR_H
#define VECTOR_H
#include <memory>
#include <cstddef>
#include <stdexcept>
#include <string>

template<typename T> class Vector_base;


template<typename T>
class Iterator
{
friend class Vector_base<T>;
Iterator(const Vector_base<T> &vec,T *t):base(vec),item(t){}
public:
Iterator<T>& operator++();
Iterator<T> operator++(int);//后缀式操作符
Iterator<T>& operator--();
Iterator<T> operator--(int);
Iterator<T> operator+(typename Vector_base<T>::size_type);
Iterator<T> operator-(typename Vector_base<T>::size_type);
bool operator!=(Iterator& iter){return item != iter.item;}
T& operator*(){return *item;}
T* operator->(){return item;}

private :
T *item;
const Vector_base<T> &base;
};

template<typename T>
Iterator<T>& Iterator<T>::operator++()
{
T *temp = item;
if(++temp > base.first_free)
throw runtime_error("out of the size ");
++item;
return *this;
}

template<typename T>
Iterator<T> Iterator<T>::operator++(int)
{
T *temp = item;
if(++temp > base.first_free)
throw std::runtime_error("out of the vector size ");
Iterator<T> ret(*this);
++item;
return ret;
}

template<typename T>
Iterator<T>& Iterator<T>::operator--()
{
if (item == base.begin)
throw runtime_error("out of the vector head");
--item;
return *this;
}

template <typename T>
Iterator<T> Iterator<T>::operator--(int)
{
if (item == base.begin)
throw runtime_error("out of the vector head");
Iterator<T> ret(*this);
++item;
return ret;
}


template<typename T>
Iterator<T> operator+(typename Vector_base<T>::size_type n)
{
Vector_base<T>::size_type s = base.first_free - item;
if (s >= n)
throw std::runtime_error("out of the vector size " );
return Iterator<T>(base,item + n);
}

template<typename T>
Iterator<T> operator-(typename Vector_base<T>::size_type n)
{
Vector_base<T>::size_type s = item - base.begin;
if (s > n)
throw std::runtime_error("out of the vector head");
return Iterator<T>(base,item-n);
}

template<typename T>
class Vector_base
{
friend class Iterator<T>;
public :
typedef std::size_t size_type;
typedef Iterator<T> Iter;

size_type size() const{return first_free - b;}
size_type capacity()const {return e - b;}


T& get(size_type n){return b[n];}
const T& get(size_type n) const {return be[n];}

void erase(size_type);
void insert(const T&);
void insert(const T&,size_type);
void reverse(size_type);
Iter beg(){return Iterator<T>(*this,b);}
Iter end(){return Iterator<T>(*this,first_free);}
public :
Vector_base():b(0),e(0),first_free(0){}
Vector_base(size_type initcapacity):b(0),e(0),first_free(0){reserve();}
virtual ~Vector_base(){}
protected :
virtual void reallocate();
virtual size_type extend()const{return size() + 1;}
protected :
T *b;
T *e;
T *first_free;
static std::allocator<T> alloc;

};

template<typename T> std::allocator<T> Vector_base<T>::alloc;

template<typename T>
void Vector_base<T>::reverse(size_type initcapacity)
{
size_type s = size();
size_type newcapacity = s + initcapacity;
T *newbegin = alloc.allocate(newcapacity);
std::uninitialized_copy(b,e,newbegin);

for (T *p = b; p != first_free ; ++p)
{
alloc.destroy(p);
}
alloc.deallocate(b,s);

b = newbegin;
first_free = b + s;
e = b + newcapacity;
}

template<typename T>
void Vector_base<T>::reallocate()
{
size_type ex = extend();
reverse(ex);
}

template<typename T>
void Vector_base<T>::insert(const T &t)
{
if(first_free == e)
reallocate();
alloc.construct(first_free,t);
++first_free;
}

template<typename T>
void Vector_base<T>::insert(const T &e,size_type p)
{
if (p < 0 || p > size())
throw runtime_error("out of range");
if(first_free == end)
reallocate();
for (T *t = first_free; t != b + p ; --t)
{
T *temp = t - 1;
alloc.constract(t,*temp)
alloc.destroy(temp);
}
alloc.construct(b + p,e);
++first_free;
}

template<typename T>
void Vector_base<T>::erase(size_type p)
{
if(p < 0 || p > size() -1)
throw std::runtime_error("out of the range");
alloc.destroy(b+p);
for (T *t = b + p; t != first_free ; ++t )
{
T *temp = t + 1;
alloc.construct(t,*(t+1));
alloc.destroy(temp);
}
--first_free;
}

template<typename T>
class Vector : public Vector_base<T>
{
public :
Vector(){}
Vector(size_type initcapacity):Vector_base<T>(initcapacity){}
};
//查找
//插入
//删除

#endif


[b]简单测试一下,没有具体详细测试,存在很多问题[/b]


#include "vector.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
Vector<int> vi;
for (int i =0;i != 10 ; i++ )
{
vi.insert(i);
}

vi.erase(7);

for (Vector<int>::Iter i = vi.beg();i != vi.end() ; )
{
cout << *i++ << endl;
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值