Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for range assign and insert for input iterator #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions MyTinySTL/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// * insert

#include <initializer_list>
#include <algorithm>

#include "iterator.h"
#include "memory.h"
Expand Down Expand Up @@ -227,7 +228,7 @@ class vector
}

void assign(std::initializer_list<value_type> il)
{ copy_assign(il.begin(), il.end(), mystl::forward_iterator_tag{}); }
{ copy_assign(il.begin(), il.end(), forward_iterator_tag{}); }

// emplace / emplace_back

Expand Down Expand Up @@ -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<iterator>(pos), first, last);
copy_insert(const_cast<iterator>(pos), first, last, iterator_category(first));
}

// erase / clear
Expand Down Expand Up @@ -315,8 +316,12 @@ class vector
// insert

iterator fill_insert(iterator pos, size_type n, const value_type& value);

template <class IIter>
void copy_insert(iterator pos, IIter first, IIter last, forward_iterator_tag);

template <class IIter>
void copy_insert(iterator pos, IIter first, IIter last);
void copy_insert(iterator pos, IIter first, IIter last, input_iterator_tag);

// shrink_to_fit

Expand Down Expand Up @@ -676,6 +681,7 @@ copy_assign(IIter first, IIter last, input_iterator_tag)
}
else
{
std::cout << "=======\n";
insert(end_, first, last);
}
}
Expand Down Expand Up @@ -817,7 +823,7 @@ fill_insert(iterator pos, size_type n, const value_type& value)
template <class T>
template <class IIter>
void vector<T>::
copy_insert(iterator pos, IIter first, IIter last)
copy_insert(iterator pos, IIter first, IIter last, forward_iterator_tag)
{
if (first == last)
return;
Expand Down Expand Up @@ -864,6 +870,18 @@ copy_insert(iterator pos, IIter first, IIter last)
}
}

template <class T>
template <class IIter>
void vector<T>::
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 <class T>
void vector<T>::reinsert(size_type size)
Expand Down
8 changes: 8 additions & 0 deletions Test/vector_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "../MyTinySTL/vector.h"
#include "test.h"
#include "stream_iterator.h"

namespace mystl
{
Expand Down Expand Up @@ -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<int>(is1), mystl::istream_iterator<int>());
COUT(v1);
v1.insert(v1.begin() + 2, mystl::istream_iterator<int>(is2), mystl::istream_iterator<int>());
COUT(v1);
PASSED;
#if PERFORMANCE_TEST_ON
std::cout << "[--------------------- Performance Testing ---------------------]\n";
Expand Down