fork download
  1. #include <iterator>
  2.  
  3. template< typename Iter >
  4. struct Iterator
  5. {
  6. using underlying_iterator = Iter;
  7.  
  8. private:
  9.  
  10. mutable underlying_iterator first,
  11. last;
  12.  
  13. public:
  14.  
  15. underlying_iterator begin() { return first; }
  16. underlying_iterator end() { return last; }
  17.  
  18. operator bool() const
  19. {
  20. return first != last;
  21. }
  22.  
  23. Iterator& operator++()
  24. {
  25. ++first;
  26. return *this;
  27. }
  28.  
  29. Iterator const& operator++() const
  30. {
  31. ++first;
  32. return *this;
  33. }
  34.  
  35. Iterator operator++(int) const
  36. {
  37. Iterator other(*this);
  38. ++first;
  39. return other;
  40. }
  41.  
  42. explicit Iterator( underlying_iterator first,
  43. underlying_iterator last ):
  44. first{first},
  45. last{last} {}
  46.  
  47. template< typename Container >
  48. Iterator(Container& c) :
  49. Iterator{ std::begin(c), std::end(c) } {}
  50.  
  51. decltype(*first) operator*()
  52. {
  53. return *first;
  54. }
  55.  
  56. decltype(*first) operator*() const
  57. {
  58. return *first;
  59. }
  60. };
  61.  
  62. #include <vector>
  63. #include <iostream>
  64.  
  65. int main()
  66. {
  67. std::vector<int> const v_const = {43, 38, 129};
  68.  
  69. Iterator<std::vector<int>::const_iterator> it_const = v_const;
  70.  
  71. while (it_const)
  72. std::cout << *it_const++ << ' ';
  73. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
43 38 129