From bd9780a1594bbe3a4cacd7133e57cf7327d90c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Tue, 23 Sep 2014 15:27:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0vector=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=AF=94=E8=BE=83=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Vector.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/TinySTL/Vector.h b/TinySTL/Vector.h index 2e5da6a..6bcff52 100644 --- a/TinySTL/Vector.h +++ b/TinySTL/Vector.h @@ -106,6 +106,10 @@ namespace TinySTL{ destroyAndDeallocateAll(); } + //比较操作相关 + bool operator == (const vector& v); + bool operator != (const vector& v); + //迭代器相关 iterator begin(){ return iterator(start_); } iterator end(){ return iterator(finish_); } @@ -194,6 +198,11 @@ namespace TinySTL{ size_type newCapacity = (oldCapacity != 0 ? (oldCapacity + res) : 1); return newCapacity; } + public: + template + friend bool operator == (const vector& v1, const vector& v2); + template + friend bool operator != (const vector& v1, const vector& v2); };// end of class vector //***********************构造,复制,析构相关*********************** @@ -379,6 +388,34 @@ namespace TinySTL{ void vector::push_back(const value_type& value){ insert(end(), value); } + //***********逻辑比较操作相关******************* + template + bool vector::operator == (const vector& v){ + if (size() != v.size()){ + return false; + } + else{ + auto ptr1 = start_; + auto ptr2 = v.start_; + for (; ptr1 != finish_ && ptr2 != v.finish_; ++ptr1, ++ptr2){ + if (*ptr1 != *ptr2) + return false; + } + return true; + } + } + template + bool vector::operator != (const vector& v){ + return !(*this == v); + } + template + bool operator == (const vector& v1, const vector& v2){ + return v1 == v2; + } + template + bool operator != (const vector& v1, const vector& v2){ + return !(v1 == v2); + } } #endif \ No newline at end of file