From 43efd7c687baebe1c26372515f37be94f5955b9d Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Mon, 28 Oct 2024 16:39:10 -0400 Subject: [PATCH] Add support for range assign and insert for input iterator --- MyTinySTL/vector.h | 26 ++++++++++++++++++++++---- Test/vector_test.h | 8 ++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/MyTinySTL/vector.h b/MyTinySTL/vector.h index 5192761..8b9ab28 100644 --- a/MyTinySTL/vector.h +++ b/MyTinySTL/vector.h @@ -17,6 +17,7 @@ // * insert #include +#include #include "iterator.h" #include "memory.h" @@ -227,7 +228,7 @@ class vector } void assign(std::initializer_list il) - { copy_assign(il.begin(), il.end(), mystl::forward_iterator_tag{}); } + { copy_assign(il.begin(), il.end(), forward_iterator_tag{}); } // emplace / emplace_back @@ -262,7 +263,7 @@ class vector void insert(const_iterator pos, Iter first, Iter last) { MYSTL_DEBUG(pos >= begin() && pos <= end() && !(last < first)); - copy_insert(const_cast(pos), first, last); + copy_insert(const_cast(pos), first, last, iterator_category(first)); } // erase / clear @@ -315,8 +316,12 @@ class vector // insert iterator fill_insert(iterator pos, size_type n, const value_type& value); + + template + void copy_insert(iterator pos, IIter first, IIter last, forward_iterator_tag); + template - void copy_insert(iterator pos, IIter first, IIter last); + void copy_insert(iterator pos, IIter first, IIter last, input_iterator_tag); // shrink_to_fit @@ -676,6 +681,7 @@ copy_assign(IIter first, IIter last, input_iterator_tag) } else { + std::cout << "=======\n"; insert(end_, first, last); } } @@ -817,7 +823,7 @@ fill_insert(iterator pos, size_type n, const value_type& value) template template void vector:: -copy_insert(iterator pos, IIter first, IIter last) +copy_insert(iterator pos, IIter first, IIter last, forward_iterator_tag) { if (first == last) return; @@ -864,6 +870,18 @@ copy_insert(iterator pos, IIter first, IIter last) } } +template +template +void vector:: +copy_insert(iterator pos, IIter first, IIter last, input_iterator_tag) { + auto off = pos - begin(); + auto old_size = size(); + for (; first != last; ++first) { + push_back(*first); + } + std::rotate(begin() + off, begin() + old_size, end()); +} + // reinsert 函数 template void vector::reinsert(size_type size) diff --git a/Test/vector_test.h b/Test/vector_test.h index 8b79ef6..bcc4500 100644 --- a/Test/vector_test.h +++ b/Test/vector_test.h @@ -7,6 +7,7 @@ #include "../MyTinySTL/vector.h" #include "test.h" +#include "stream_iterator.h" namespace mystl { @@ -90,6 +91,13 @@ void vector_test() FUN_AFTER(v1, v1.shrink_to_fit()); FUN_VALUE(v1.size()); FUN_VALUE(v1.capacity()); + + std::istringstream is1("0 0 0 0"); + std::istringstream is2("1 2 3 4 5 6 7 8 9"); + v1.assign(mystl::istream_iterator(is1), mystl::istream_iterator()); + COUT(v1); + v1.insert(v1.begin() + 2, mystl::istream_iterator(is2), mystl::istream_iterator()); + COUT(v1); PASSED; #if PERFORMANCE_TEST_ON std::cout << "[--------------------- Performance Testing ---------------------]\n";