From 57971e0dfc567f52fc9df5187c24ab61972ee8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 20 Sep 2014 13:30:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90vector=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E5=A4=8D=E5=88=B6=E6=9E=84=E9=80=A0=E5=92=8C=E6=9E=90?= =?UTF-8?q?=E6=9E=84=E7=9B=B8=E5=85=B3=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Vector.h | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/TinySTL/Vector.h b/TinySTL/Vector.h index 060c01a..524bf70 100644 --- a/TinySTL/Vector.h +++ b/TinySTL/Vector.h @@ -1,6 +1,8 @@ #ifndef _VECTOR_H_ #define _VECTOR_H_ +#include + #include "Allocator.h" #include "Iterator.h" #include "UninitializedFunctions.h" @@ -71,7 +73,7 @@ namespace TinySTL{ explicit vector(const size_type n); vector(const size_type n, const value_type& value); template - vector(InputIterator first, InputIterator last); + vector(InputIterator first, InputIterator last); vector(const vector& v); vector(vector&& v); vector& operator = (const vector& v); @@ -83,6 +85,12 @@ namespace TinySTL{ //迭代器相关 iterator begin(){ return iterator(start_); } iterator end(){ return iterator(finish_); } + + //修改容器相关的操作 + void clear(){ + dataAllocator::destroy(start_, finish_); + finish_ = start_; + } private: void allocateAndFillN(const size_type n, const value_type& value){ start_ = dataAllocator::allocate(n); @@ -95,8 +103,17 @@ namespace TinySTL{ finish_ = uninitialized_copy(first, last, start_); endOfStorage_ = finish_; } - }; + template + void vector_aux(InputIterator first, InputIterator last, std::false_type){ + allocateAndCopy(first, last); + } + template + void vector_aux(Integer n, Integer value, std::true_type){ + allocateAndFillN(n, value); + } + }; + //***********************构造,复制,析构相关*********************** template vector::vector(const size_type n){ allocateAndFillN(n, value_type()); @@ -106,9 +123,28 @@ namespace TinySTL{ allocateAndFillN(n, value); } template - template + template vector::vector(InputIterator first, InputIterator last){ - allocateAndCopy(first, last); + //处理指针和数字间的区别的函数 + vector_aux(first, last, typename std::is_integral::type()); + } + template + vector::vector(const vector& v){ + allocateAndCopy(v.start_, v.finish_); + } + template + vector::vector(vector&& v){ + start_ = v.start_; + finish_ = v.finish_; + endOfStorage_ = v.endOfStorage_; + v.clear(); + } + template + vector& vector::operator = (const vector& v){ + if (this != &v){ + allocateAndCopy(v.start_, v.finish_); + } + return *this; } }