Advertisement
playerr17

Untitled

Oct 2nd, 2022 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <forward_list>
  5. #include <string>
  6. #include <utility>
  7.  
  8. using Value = std::string;
  9. using Iterator = std::forward_list<std::string>::const_iterator;
  10. using ZippedPair = std::pair<const Value&, const Value&>;
  11.  
  12. class Zipped {
  13. public:
  14.     class ZippedIterator {
  15.     public:
  16.         ZippedIterator(Iterator& first, Iterator& second) : first_(first), second_(second) {};
  17.  
  18.         ZippedIterator& operator++() {
  19.             ++first_;
  20.             ++second_;
  21.             return *this;
  22.         }
  23.  
  24.         ZippedIterator operator++(int) {
  25.             ZippedIterator now(*this);
  26.             ++first_;
  27.             ++second_;
  28.             return now;
  29.         }
  30.  
  31.         bool operator!=(const ZippedIterator& rhs) {
  32.             return first_ != rhs.first_ && second_ != rhs.second_;
  33.         }
  34.  
  35.         ZippedPair& operator*() {
  36.             return *new ZippedPair(std::make_pair(*first_, *second_));
  37.         }
  38.  
  39.     private:
  40.         Iterator& first_;
  41.         Iterator& second_;
  42.     };
  43.  
  44.     Zipped(Iterator a_begin, Iterator a_end, Iterator b_begin, Iterator b_end)
  45.         : a_begin_(a_begin), a_end_(a_end), b_begin_(b_begin), b_end_(b_end) {}
  46.  
  47.     ZippedIterator begin() {
  48.         return ZippedIterator(a_begin_, b_begin_);
  49.     }
  50.  
  51.     ZippedIterator end() {
  52.         return ZippedIterator(a_end_, b_end_);
  53.     }
  54.  
  55. private:
  56.     Iterator a_begin_;
  57.     Iterator a_end_;
  58.     Iterator b_begin_;
  59.     Iterator b_end_;
  60. };
  61.  
  62. Zipped Zip(Iterator a_begin, Iterator a_end, Iterator b_begin, Iterator b_end) {
  63.     return Zipped(a_begin, a_end, b_begin, b_end);
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement