fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iterator>
  5.  
  6. namespace internal
  7. {
  8. class PREORDER {};
  9. class POSTORDER {};
  10.  
  11. // ----------------------------------------------------------------------------------
  12. template<typename P, typename IT>
  13. class my_iterator;
  14.  
  15.  
  16. template<typename P>
  17. class my_iterator<P, PREORDER>
  18. : public std::iterator<std::forward_iterator_tag, P>
  19. {
  20. private:
  21. int index;
  22. protected:
  23. public:
  24. my_iterator () { index = 0; }
  25.  
  26. my_iterator& operator++ () {
  27. std::cout << "Preorder visit of GenericTree of nodes containing int values." << std::endl;
  28. return (*this);
  29. }
  30. };
  31. // ----------------------------------------------------------------------------------
  32. template<typename P>
  33. class my_iterator<P, POSTORDER>
  34. : public std::iterator<std::forward_iterator_tag, P>
  35. {
  36. private:
  37. int index;
  38. protected:
  39. public:
  40. my_iterator () { index = 0; }
  41.  
  42. my_iterator& operator++ () {
  43. std::cout << "Postorder visit of GenericTree of nodes containing int values." << std::endl;
  44. return (*this);
  45. }
  46. };
  47.  
  48. }
  49.  
  50. template<typename P> class GenericTree
  51. {
  52. public:
  53. template <typename IT>
  54. using my_iterator = internal::my_iterator<P, IT>;
  55. using PREORDER = internal::PREORDER;
  56. using POSTORDER = internal::POSTORDER;
  57.  
  58. std::vector<P> data;
  59. GenericTree () {}
  60. ~GenericTree () {}
  61. };
  62.  
  63.  
  64.  
  65. int main ()
  66. {
  67. GenericTree<int> c_int;
  68. GenericTree<int>::my_iterator<GenericTree<int>::PREORDER> iterator_int;
  69.  
  70. ++iterator_int;
  71.  
  72. GenericTree<std::string> c_string;
  73. GenericTree<std::string>::my_iterator<GenericTree<std::string>::POSTORDER> iterator_str;
  74.  
  75. ++iterator_str;
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Preorder visit of GenericTree of nodes containing int values.
Postorder visit of GenericTree of nodes containing int values.