diff --git a/TinySTL/Detail/List.impl.h b/TinySTL/Detail/List.impl.h index aeb4e84..4735ede 100644 --- a/TinySTL/Detail/List.impl.h +++ b/TinySTL/Detail/List.impl.h @@ -395,7 +395,7 @@ namespace TinySTL{ auto node1 = lhs.head.p, node2 = rhs.head.p; for (; node1 != lhs.tail.p && node2 != rhs.tail.p; node1 = node1->next, node2 = node2->next){ if (node1->data != node2->data) - break; + return false; } if (node1 == lhs.tail.p && node2 == rhs.tail.p) return true; diff --git a/TinySTL/Detail/String.cpp b/TinySTL/Detail/String.cpp index 2f38c69..868cadd 100644 --- a/TinySTL/Detail/String.cpp +++ b/TinySTL/Detail/String.cpp @@ -243,7 +243,9 @@ namespace TinySTL{ } size_t string::find(const string& str, size_t pos) const{ size_t lengthOfS = str.size(); - if (size() - pos < lengthOfS) + //if (size() - pos < lengthOfS) + // return npos; + if (size() < lengthOfS + pos) return npos; return find_aux(str.cbegin(), pos, lengthOfS, size()); } @@ -262,6 +264,7 @@ namespace TinySTL{ } size_t string::rfind(char c, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); + if (npos == pos) return npos; for (auto cit = cbegin() + pos; cit >= cbegin(); --cit){ if (*cit == c) return cit - cbegin(); @@ -288,10 +291,12 @@ namespace TinySTL{ //if (pos - 0 < lengthOfS) // return npos; pos = changeVarWhenEuqalNPOS(pos, size(), 1); + if (npos == pos) return npos; return rfind_aux(str.begin(), pos, lengthOfS, 0); } size_t string::rfind(const char* s, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); + if (npos == pos) return npos; return rfind(s, pos, strlen(s)); } size_t string::rfind(const char* s, size_t pos, size_t n) const{ @@ -377,7 +382,8 @@ namespace TinySTL{ return npos; } size_t string::find_first_not_of(char c, size_t pos) const{ - for (size_t i = pos; i != size(); ++i){ + //bug fix (if pos>size()) + for (size_t i = pos; i < size(); ++i){ if ((*this)[i] != c) return i; } @@ -386,6 +392,7 @@ namespace TinySTL{ size_t string::find_last_of(const string& str, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); //return find_last_of(str.begin(), pos, pos + 1); + if (npos == pos) return npos; return find_last_of(str.begin(), pos, str.size()); } size_t string::find_last_of(const char* s, size_t pos) const{ @@ -412,11 +419,13 @@ namespace TinySTL{ size_t string::find_last_not_of(const string& str, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); //return find_last_not_of(str.begin(), pos, size()); + if (npos == pos) return npos; return find_last_not_of(str.begin(), pos, str.size()); } size_t string::find_last_not_of(const char* s, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); //return find_last_not_of(s, pos, pos + 1); + if (npos == pos) return npos; return find_last_not_of(s, pos, strlen(s)); } size_t string::find_last_not_of(const char* s, size_t pos, size_t n) const{ diff --git a/TinySTL/Detail/Vector.impl.h b/TinySTL/Detail/Vector.impl.h index 55b3097..e1a6c23 100644 --- a/TinySTL/Detail/Vector.impl.h +++ b/TinySTL/Detail/Vector.impl.h @@ -63,7 +63,8 @@ namespace TinySTL{ } else if (n > capacity()){ auto lengthOfInsert = n - size(); - T *newStart = dataAllocator::allocate(getNewCapacity(lengthOfInsert)); + //T *newStart = dataAllocator::allocate(getNewCapacity(lengthOfInsert)); + T *newStart = dataAllocator::allocate(getNewCapacity(n)); T *newFinish = TinySTL::uninitialized_copy(begin(), end(), newStart); newFinish = TinySTL::uninitialized_fill_n(newFinish, lengthOfInsert, val); @@ -88,20 +89,19 @@ namespace TinySTL{ //***************修改容器的相关操作************************** template typename vector::iterator vector::erase(iterator position){ - return erase(position, position + 1); + if (position + 1 != end()) + std::copy(position + 1, finish_, position); + auto tmp = finish_; + --finish_; + dataAllocator::destroy(tmp); + return position; } template typename vector::iterator vector::erase(iterator first, iterator last){ - //尾部残留对象数 - difference_type lenOfTail = end() - last; - //删去的对象数目 - difference_type lenOfRemoved = last - first; - finish_ = finish_ - lenOfRemoved; - for (; lenOfTail != 0; --lenOfTail){ - auto temp = (last - lenOfRemoved); - *temp = *(last++); - } - return (first); + iterator pos = std::copy(last, finish_, first); + dataAllocator::destroy(pos, finish_); + finish_ = finish_ - (last - first); + return first; } template template