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

_error_deque::insert_dispatch #159

Open
wants to merge 2 commits 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
5 changes: 2 additions & 3 deletions MyTinySTL/deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions MyTinySTL/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ struct has_iterator_cat_of
template <class T, class U>
struct has_iterator_cat_of<T, U, false> : public m_false_type {};

template <class Iter>
struct is_exactly_input_iterator : public m_bool_constant<has_iterator_cat_of<Iter, input_iterator_tag>::value &&
!has_iterator_cat_of<Iter, forward_iterator_tag>::value> {};

template <class Iter>
struct is_input_iterator : public has_iterator_cat_of<Iter, input_iterator_tag> {};

Expand Down
80 changes: 80 additions & 0 deletions MyTinySTL/stream_iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#ifndef MYTINYSTL_STREAM_ITERATOR_H_
#define MYTINYSTL_STREAM_ITERATOR_H_

#include "basic_string.h"

namespace mystl
{

template<typename T, typename CharT = char,
typename Traits = std::char_traits<CharT>, typename Dist = ptrdiff_t>
class istream_iterator
: public iterator<input_iterator_tag, T, Dist, const T*, const T&>
{
public:
using char_type = CharT;
using traits_type = Traits;
using istream_type = std::basic_istream<CharT, Traits>;

istream_iterator() /* noexcept(std::is_nothrow_default_constructible<T>::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<T>::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<typename T, typename CharT = char,
// typename Traits = char_traits<CharT> >
// class ostream_iterator : public iterator<output_iterator_tag, void, void, void, void> {};
}

#endif
18 changes: 17 additions & 1 deletion Test/vector_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include "../MyTinySTL/vector.h"
#include "test.h"
#include "../MyTinySTL/stream_iterator.h"
#include <iostream>
#include <iterator>
#include <sstream>

namespace mystl
{
Expand All @@ -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<int> beg(inputStream), end;
mystl::vector<int> 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));
Expand Down