From 4072727feb8debbad1fa6ead894f3e4efb8b762d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 10 Oct 2014 19:05:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A7=BB=E5=8A=A8=E8=AF=AD?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/String.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/TinySTL/String.h b/TinySTL/String.h index 8efe4c2..da42d1e 100644 --- a/TinySTL/String.h +++ b/TinySTL/String.h @@ -33,6 +33,7 @@ namespace TinySTL{ public: string() :start_(0), finish_(0), endOfStorage_(0){} string(const string& str); + string(string&& str); string(const string& str, size_t pos, size_t len = npos); string(const char* s); string(const char* s, size_t n); @@ -41,6 +42,7 @@ namespace TinySTL{ string(InputIterator first, InputIterator last); string& operator= (const string& str); + string& operator= (string&& str); string& operator= (const char* s); string& operator= (char c); @@ -166,6 +168,12 @@ namespace TinySTL{ int compare(size_t pos, size_t len, const char* s) const; int compare(size_t pos, size_t len, const char* s, size_t n) const; private: + void moveData(string& str){ + start_ = str.start_; + finish_ = str.finish_; + endOfStorage_ = str.endOfStorage_; + str.start_ = str.finish_ = str.endOfStorage_ = 0; + } //插入时空间不足的情况 template iterator insert_aux_copy(iterator p, InputIterator first, InputIterator last); @@ -201,6 +209,7 @@ namespace TinySTL{ } size_t rfind_aux(const_iterator cit, size_t pos, size_t lengthOfS, size_t cond)const; size_t find_aux(const_iterator cit, size_t pos, size_t lengthOfS, size_t cond)const; + int compare_aux(size_t pos, size_t len, const_iterator cit, size_t subpos, size_t sublen)const; public: friend std::ostream& operator <<(std::ostream& os, const string&str); };// end of string @@ -224,6 +233,9 @@ namespace TinySTL{ string::string(const string& str){ allocateAndCopy(str.start_, str.finish_); } + string::string(string&& str){ + moveData(str); + } string::string(const string& str, size_t pos, size_t len){ allocateAndCopy(str.start_ + pos, str.start_ + pos + len); } @@ -234,6 +246,12 @@ namespace TinySTL{ } return *this; } + string& string::operator= (string&& str){ + if (this != &str){ + moveData(str); + } + return *this; + } string& string::operator= (const char* s){ destroyAndDeallocate(); allocateAndCopy(s, s + strlen(s)); @@ -516,6 +534,39 @@ namespace TinySTL{ return npos; return rfind_aux(s, pos, lengthOfS, pos - n); } + int string::compare(const string& str) const{ + return compare(0, size(), str, 0, str.size()); + } + int string::compare(size_t pos, size_t len, const string& str) const{ + return compare(pos, len, str, 0, str.size()); + } + int string::compare_aux(size_t pos, size_t len, const_iterator cit, size_t subpos, size_t sublen)const{ + size_t i, j; + for (i = 0, j = 0; i != len && j != sublen; ++i, ++j){ + if ((*this)[pos + i] < cit[subpos + j]) + return -1; + else if ((*this)[pos + i] > cit[subpos + j]) + return 1; + } + if (i == len && j == sublen) + return 0; + else if (i == len) + return -1; + else if (j == sublen) + return 1; + } + int string::compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const{ + return compare_aux(pos, len, str.begin(), subpos, sublen); + } + int string::compare(const char* s) const{ + return compare(0, size(), s, strlen(s)); + } + int string::compare(size_t pos, size_t len, const char* s) const{ + return compare(pos, len, s, strlen(s)); + } + int string::compare(size_t pos, size_t len, const char* s, size_t n) const{ + return compare_aux(pos, len, s, 0, n); + } std::ostream& operator <<(std::ostream& os, const string&str){ for (const auto ch : str){ os << ch;