diff --git a/TinySTL/Detail/Vector.impl.h b/TinySTL/Detail/Vector.impl.h index d2cb2e8e060955f9ca06fd3c6cc7a29df7fcd2ea..e2d26efbb05af2d9d5a5c70696ce1ca4db174f79 100644 --- a/TinySTL/Detail/Vector.impl.h +++ b/TinySTL/Detail/Vector.impl.h @@ -144,12 +144,16 @@ namespace TinySTL{ difference_type locationNeed = distance(first, last);//last - first; if (locationLeft >= locationNeed){ - iterator tempPtr = end() - 1; - for (; tempPtr - position >= 0; --tempPtr){//move the [position, finish_) back - //*(tempPtr + locationNeed) = *tempPtr;//bug - construct(tempPtr + locationNeed, *tempPtr); + if (finish_ - position > locationNeed){ + TinySTL::uninitialized_copy(finish_ - locationNeed, finish_, finish_); + std::copy_backward(position, finish_ - locationNeed, finish_); + std::copy(first, last, position); + } + else{ + iterator temp = TinySTL::uninitialized_copy(first + (finish_ - position), last, finish_); + TinySTL::uninitialized_copy(position, finish_, temp); + std::copy(first, first + (finish_ - position), position); } - TinySTL::uninitialized_copy(first, last, position); finish_ += locationNeed; } else{ diff --git a/TinySTL/Test/VectorTest.cpp b/TinySTL/Test/VectorTest.cpp index 514b522258cd4b103bc20c889d969887455ae6bd..f4783403282dc68a5f8bc9ecbe2e5e82a5248d05 100644 --- a/TinySTL/Test/VectorTest.cpp +++ b/TinySTL/Test/VectorTest.cpp @@ -186,7 +186,48 @@ namespace TinySTL{ assert(!(foo == bar)); assert(foo != bar); } + + class TestItem + { + public: + TestItem() + { + ++count; + } + TestItem(const TestItem & other) + { + ++count; + } + virtual ~TestItem() + { + --count; + } + + static int getCount() + { + return count; + } + private: + static int count; + }; + int TestItem::count = 0; + + void testCase15() + { + assert(TestItem::getCount() == 0); + { + typedef TinySTL::vector TVector; + TVector t(10); + t.push_back(TestItem()); + t.push_back(TestItem()); + t.push_back(TestItem()); + t.insert(t.begin(), t.begin(), t.begin() + 1); + + } + assert(TestItem::getCount() == 0); + + } void testAllCases(){ testCase1(); @@ -203,6 +244,7 @@ namespace TinySTL{ testCase12(); testCase13(); testCase14(); + testCase15(); } } } \ No newline at end of file