From 6f8c977082677073dbdc8c21ad385bd43d1795ab 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 17:57:42 +0800 Subject: [PATCH] insert bug fix --- TinySTL/String.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/TinySTL/String.h b/TinySTL/String.h index 2125769..8efe4c2 100644 --- a/TinySTL/String.h +++ b/TinySTL/String.h @@ -298,9 +298,8 @@ namespace TinySTL{ auto lengthOfLeft = capacity() - size(); size_t lengthOfInsert = last - first; if (lengthOfInsert <= lengthOfLeft){ - auto ptr = finish_; - for (auto i = lengthOfInsert; i != 0; --i){ - *(finish_ - 1 + i) = *(--ptr); + for (iterator it = finish_ - 1; it >= p; --it){ + *(it + lengthOfInsert) = *(it); } TinySTL::uninitialized_copy(first, last, p); finish_ += lengthOfInsert; @@ -346,9 +345,8 @@ namespace TinySTL{ string::iterator string::insert(iterator p, size_t n, char c){ auto lengthOfLeft = capacity() - size(); if (n <= lengthOfLeft){ - auto ptr = finish_; - for (auto i = n; i != 0; --i){ - *(finish_ - 1 + i) = *(--ptr); + for (iterator it = finish_ - 1; it >= p; --it){ + *(it + n) = *(it); } TinySTL::uninitialized_fill_n(p, n, c); finish_ += n; @@ -405,6 +403,7 @@ namespace TinySTL{ dataAllocator::destroy(first + lengthOfMove, finish_); finish_ = first + lengthOfMove; return first; + } string& string::erase(size_t pos, size_t len){ erase(begin() + pos, begin() + pos + len); @@ -518,8 +517,9 @@ namespace TinySTL{ return rfind_aux(s, pos, lengthOfS, pos - n); } std::ostream& operator <<(std::ostream& os, const string&str){ - for (const auto ch : str) + for (const auto ch : str){ os << ch; + } return os; } }