diff --git a/MyTinySTL/deque.h b/MyTinySTL/deque.h index 7975d83..dc256e9 100644 --- a/MyTinySTL/deque.h +++ b/MyTinySTL/deque.h @@ -1245,10 +1245,9 @@ insert_dispatch(iterator position, IIter first, IIter last, input_iterator_tag) require_capacity(n, false); } position = begin_ + elems_before; - auto cur = --last; - for (size_type i = 0; i < n; ++i, --cur) + for (; first != last; ++first, ++position) { - insert(position, *cur); + insert(position, *first); } } diff --git a/MyTinySTL/iterator.h b/MyTinySTL/iterator.h index 28a9a69..a3e5cfa 100644 --- a/MyTinySTL/iterator.h +++ b/MyTinySTL/iterator.h @@ -103,6 +103,10 @@ struct has_iterator_cat_of template struct has_iterator_cat_of : public m_false_type {}; +template +struct is_exactly_input_iterator : public m_bool_constant::value && + !has_iterator_cat_of::value> {}; + template struct is_input_iterator : public has_iterator_cat_of {}; diff --git a/MyTinySTL/stream_iterator.h b/MyTinySTL/stream_iterator.h new file mode 100644 index 0000000..0da2140 --- /dev/null +++ b/MyTinySTL/stream_iterator.h @@ -0,0 +1,80 @@ +#ifndef MYTINYSTL_STREAM_ITERATOR_H_ +#define MYTINYSTL_STREAM_ITERATOR_H_ + +#include "basic_string.h" + +namespace mystl +{ + +template, typename Dist = ptrdiff_t> +class istream_iterator +: public iterator +{ +public: + using char_type = CharT; + using traits_type = Traits; + using istream_type = std::basic_istream; + + istream_iterator() /* noexcept(std::is_nothrow_default_constructible::value) */ + : m_stream{nullptr}, m_value{} {} + + istream_iterator(istream_type& is) + : m_stream{std::addressof(is)} + { read(); } + + istream_iterator(const istream_iterator& other) /* noexcept(std::is_nothrow_copy_constructible::value) */ + = default; // memberwise copy + + istream_iterator& operator=(const istream_iterator&) = default; // memberwise copy-asgn + + ~istream_iterator() = default; + + const T& operator*() const noexcept { + MYSTL_DEBUG(m_stream != nullptr); + return m_value; + } + + const T* operator->() const noexcept { + return std::addressof(*this); + } + + istream_iterator& operator++() { + MYSTL_DEBUG(m_stream != nullptr); + read(); + return *this; + } + + istream_iterator operator++(int) { + auto tmp = *this; + ++*this; + return tmp; + } + +private: + istream_type* m_stream; + T m_value; + + void read() { + if (m_stream && !(*m_stream >> m_value)) { // m_stream 有效且读到 EOS + m_stream = nullptr; + } + } + + friend bool operator==(const istream_iterator& lhs, const istream_iterator& rhs) { + return lhs.m_stream == rhs.m_stream; + } + + friend bool operator!=(const istream_iterator& lhs, const istream_iterator& rhs) { + return lhs.m_stream != rhs.m_stream; + } +}; + + +// TODO +// template > +// class ostream_iterator : public iterator {}; +} + +#endif \ No newline at end of file diff --git a/Test/vector_test.h b/Test/vector_test.h index 8b79ef6..e230203 100644 --- a/Test/vector_test.h +++ b/Test/vector_test.h @@ -7,6 +7,10 @@ #include "../MyTinySTL/vector.h" #include "test.h" +#include "../MyTinySTL/stream_iterator.h" +#include +#include +#include namespace mystl { @@ -33,7 +37,19 @@ void vector_test() v9 = std::move(v3); v10 = { 1,2,3,4,5,6,7,8,9 }; - FUN_AFTER(v1, v1.assign(8, 8)); + //输入迭代器测试 + std::istringstream inputStream("6 2 3 4 5 5 7"); + mystl::istream_iterator beg(inputStream), end; + mystl::vector v11({9,8,7,6,5,4}); + v11.assign(beg, end); + int count = 0; + for(beg; beg!=end; ++beg) + std::cout<<*beg; + std::cout << std::endl; + std::cout << *beg << std::endl; + std::cout << *beg << std::endl; + COUT(v11); + FUN_AFTER(v1, v1.assign(a, a + 5)); FUN_AFTER(v1, v1.emplace(v1.begin(), 0)); FUN_AFTER(v1, v1.emplace_back(6));