Advertisement
andrei_gavrila

Design Patterns - Iterator

Nov 28th, 2021
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  iterator
  4. //
  5. //  Created by Andrei Gavrila on 28/11/2021.
  6. //  Copyright © 2021 Andrei Gavrila. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <algorithm>
  11.  
  12. template <typename T>
  13. class Node
  14. {
  15. public:
  16.     Node *next;
  17.    
  18.     T value;
  19.  
  20.     Node(Node<T>* next, T value): next(next), value(value) {}
  21. };
  22.  
  23. template <typename T>
  24. class List
  25. {
  26. private:
  27.     Node<T> *head;
  28.  
  29. public:
  30.     struct Iterator
  31.     {
  32.         Iterator(Node<T> *node) : node(node) {}
  33.  
  34.         T& operator*() const { return node->value; }
  35.         T operator->() { return node->value; }
  36.         Iterator& operator++() { node = node->next; return *this; }
  37.         Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }
  38.  
  39.         friend bool operator== (const Iterator& a, const Iterator& b) { return a.node == b.node; };
  40.         friend bool operator!= (const Iterator& a, const Iterator& b) { return a.node != b.node; };
  41.  
  42.     private:
  43.         Node<T> *node;
  44.     };
  45.  
  46.     List(): head(nullptr) {}
  47.  
  48.     void add(T value)
  49.     {
  50.         head = new Node<T>(head, value);
  51.     }
  52.  
  53.     void print()
  54.     {
  55.         Node<T> *n = head;
  56.  
  57.         std::cout << "List: [" ;
  58.         while (n)
  59.         {
  60.             std::cout << n->value << (n->next ? ", " : "");
  61.  
  62.             n = n->next;
  63.         }
  64.         std::cout << "]" << std::endl;
  65.     }
  66.  
  67.     Iterator begin() {
  68.         return Iterator(head);
  69.     }
  70.  
  71.     Iterator end()
  72.     {
  73.         return Iterator(nullptr);
  74.     }
  75. };
  76.  
  77. int main(int argc, const char * argv[])
  78. {
  79.     List<std::string> *list = new List<std::string>();
  80.  
  81.     list->add("element 1");
  82.     list->add("element 2");
  83.     list->add("element 3");
  84.  
  85.     list->print();
  86.  
  87.     for (auto it = list->begin(), end = list->end(); it != end; ++it) {
  88.         const auto s = *it;
  89.  
  90.         std::cout << s << "\n";
  91.     }
  92.  
  93.     for (auto s: *list)
  94.     {
  95.         std::cout << s << "\n";
  96.     }
  97.  
  98.     List<int> *listOfInts = new List<int>();
  99.  
  100.     listOfInts->add(1);
  101.     listOfInts->add(2);
  102.     listOfInts->add(3);
  103.  
  104.     int sum = 0;
  105.     for (auto n: *listOfInts)
  106.     {
  107.         sum += n;
  108.     }
  109.     std::cout << "Sum = " << sum << std::endl;
  110.  
  111.     int sum_quick = 0;
  112.     std::for_each(listOfInts->begin(), listOfInts->end(), [&](int const& n) {sum_quick += n;});
  113.     std::cout << "Sum = " << sum_quick << std::endl;
  114.  
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement