Advertisement
DNKpp

Untitled

Aug 8th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. template <class _Ty, class _Elem = char, class _Traits = std::char_traits<_Elem>, class _Diff = std::ptrdiff_t>
  7. class Itr
  8. { // wrap _Ty extracts from input stream as input iterator
  9. public:
  10.     using iterator_category = std::input_iterator_tag;
  11.     using value_type        = _Ty;
  12.     using difference_type   = _Diff;
  13.     using pointer           = const _Ty*;
  14.     using reference         = const _Ty &;
  15.  
  16.     using char_type    = _Elem;
  17.     using traits_type  = _Traits;
  18.     using istream_type = std::basic_istream<_Elem, _Traits>;
  19.  
  20.     constexpr Itr() : _Myistr(nullptr), _Myval() { // construct singular iterator
  21.     }
  22.  
  23.     Itr(istream_type& _Istr) : _Myistr(std::addressof(_Istr)) { // construct with input stream
  24.         _Getval();
  25.     }
  26.  
  27.     const _Ty& operator*() const {
  28.         return _Myval;
  29.     }
  30.  
  31.     const _Ty* operator->() const {
  32.         return std::addressof(_Myval);
  33.     }
  34.  
  35.     Itr& operator++() {
  36.         _Getval();
  37.         return *this;
  38.     }
  39.  
  40.     Itr operator++(int) {
  41.         auto _Tmp = *this;
  42.         _Getval();
  43.         return _Tmp;
  44.     }
  45.  
  46.     bool _Equal(const Itr& _Right) const {
  47.         return _Myistr == _Right._Myistr;
  48.     }
  49.  
  50. protected:
  51.     void _Getval() { // get a _Ty value if possible
  52.  
  53.         if (_Myistr)
  54.         {
  55.             while (!(*_Myistr >> _Myval))
  56.             {
  57.                 if (_Myistr->eof())
  58.                 {
  59.                     _Myistr = nullptr;
  60.                     break;
  61.                 }
  62.                 else
  63.                 {
  64.                     _Myistr->clear();
  65.                     _Myistr->get();
  66.                 }
  67.             }
  68.         }
  69.     }
  70.  
  71.     istream_type* _Myistr; // pointer to input stream
  72.     _Ty _Myval; // lookahead value (valid if _Myistr is not null)
  73. };
  74.  
  75. template <class _Ty, class _Elem, class _Traits, class _Diff>
  76. inline bool operator==(const Itr<_Ty, _Elem, _Traits, _Diff>& _Left,
  77.     const Itr<_Ty, _Elem, _Traits, _Diff>& _Right) { // test for istream_iterator equality
  78.     return _Left._Equal(_Right);
  79. }
  80.  
  81. template <class _Ty, class _Elem, class _Traits, class _Diff>
  82. inline bool operator!=(const Itr<_Ty, _Elem, _Traits, _Diff>& _Left,
  83.     const Itr<_Ty, _Elem, _Traits, _Diff>& _Right) { // test for istream_iterator inequality
  84.     return !(_Left == _Right);
  85. }
  86.  
  87. void fill_vector(std::istream& ist, std::vector<int>& v)
  88. {
  89.     std::copy(Itr<int>(ist), Itr<int>(), std::back_inserter(v));
  90. }
  91.  
  92. int main()
  93. {
  94.     std::vector<int> v;
  95.     fill_vector(std::cin, v);
  96.    
  97.  
  98.     std::copy(std::begin(v), std::end(v), std::ostream_iterator<int>(std::cout, ","));
  99.  
  100.     std::cin.clear();
  101.     std::cin.get();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement