fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <typeinfo>
  6. #include <type_traits>
  7. #include <tuple>
  8. #include <functional>
  9. #include <string>
  10. #include <list>
  11.  
  12. #include <iterator>
  13.  
  14.  
  15. template <typename T> class VectorList;
  16.  
  17. template <typename ValueType>
  18. class Iterator : public std::iterator<std::forward_iterator_tag, ValueType>
  19. {
  20. friend class VectorList<ValueType>;
  21. private:
  22. Iterator(typename std::list<std::vector<ValueType>>::iterator it);
  23.  
  24. public:
  25. Iterator(Iterator const& other);
  26.  
  27. Iterator & operator++();
  28. typename Iterator::reference operator*() const;
  29. typename Iterator::pointer operator->() const;
  30. bool operator!=(Iterator const& other) const;
  31.  
  32. private:
  33. typename std::list<std::vector<ValueType>>::iterator data;
  34. };
  35.  
  36. template <typename ValueType>
  37. Iterator<ValueType>::Iterator(typename std::list<std::vector<ValueType>>::iterator it)
  38. : data(it)
  39. {
  40.  
  41. }
  42.  
  43. template <typename ValueType>
  44. Iterator<ValueType>::Iterator(Iterator const& other)
  45. : data(other.data)
  46. {
  47.  
  48. }
  49.  
  50. template <typename ValueType>
  51. Iterator<ValueType> & Iterator<ValueType>::operator++()
  52. {
  53. ++data;
  54. return *this;
  55. }
  56.  
  57. template <typename ValueType>
  58. typename Iterator<ValueType>::reference Iterator<ValueType>::operator*() const
  59. {
  60. return *data;
  61. }
  62.  
  63. template <typename ValueType>
  64. typename Iterator<ValueType>::pointer Iterator<ValueType>::operator->() const
  65. {
  66. return &this->operator*();
  67. }
  68.  
  69. template <typename ValueType>
  70. bool Iterator<ValueType>::operator!=(Iterator const& other) const
  71. {
  72. return data != other.data;
  73. }
  74.  
  75. template<class T>
  76. class VectorList
  77. {
  78. private:
  79. using VectT = std::vector<T>;
  80. using ListT = std::list<VectT>;
  81.  
  82. public:
  83. using value_type = T;
  84. using iterator = Iterator<T>;
  85. using const_iterator = Iterator<const T>;
  86.  
  87. VectorList() = default;
  88. VectorList(VectorList const &) = default;
  89. VectorList(VectorList &&) = default;
  90.  
  91. VectorList & operator=(VectorList &&) = default;
  92. VectorList & operator=(VectorList const &) = default;
  93.  
  94. template<class It>
  95. void append(It p, It q);
  96.  
  97. bool empty() const { return size() == 0; }
  98.  
  99. size_t size() const;
  100.  
  101. iterator begin();
  102. iterator end();
  103.  
  104.  
  105. private:
  106. ListT data_;
  107. };
  108.  
  109. template <typename T>
  110. template <typename It>
  111. void VectorList<T>::append(It p, It q)
  112. {
  113. if (p != q)
  114. data_.push_back(VectT(p, q));
  115. }
  116.  
  117. template <typename T>
  118. size_t VectorList<T>::size() const
  119. {
  120. size_t ret = 0;
  121. for (const VectT& vect : data_)
  122. {
  123. ret += vect.size();
  124. }
  125. return ret;
  126. }
  127.  
  128. template <typename T>
  129. typename VectorList<T>::iterator VectorList<T>::begin()
  130. {
  131. //return Iterator(data_.begin());
  132. }
  133.  
  134. template <typename T>
  135. typename VectorList<T>::iterator VectorList<T>::end()
  136. {
  137. //return Iterator(data_.end());
  138. }
  139.  
  140.  
  141.  
  142. int main()
  143. {
  144. VectorList<int> vl;
  145. std::vector<int> a = { 1, 2, 3 };
  146. std::vector<int> b = { 4, 5, 6 };
  147. vl.append(a.begin(), a.end());
  148. vl.append(b.begin(), b.end());
  149.  
  150. for (auto it : vl)
  151. {
  152. std::cout << it << ' ';
  153. }
  154.  
  155. return 0;
  156. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘typename Iterator<ValueType>::reference Iterator<ValueType>::operator*() const [with ValueType = int; typename Iterator<ValueType>::reference = int&]’:
prog.cpp:150:17:   required from here
prog.cpp:60:10: error: invalid initialization of reference of type ‘std::iterator<std::forward_iterator_tag, int, long int, int*, int&>::reference {aka int&}’ from expression of type ‘std::vector<int, std::allocator<int> >’
  return *data;
          ^~~~
stdout
Standard output is empty