C++ Vector的简单实现
vector.h的代码
#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简单测试一下,没有具体详细测试,存在很多问题
#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;}}