fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<typename T>
  5. struct myIterator : public std::iterator<std::input_iterator_tag, T*>
  6. {
  7. myIterator(T* data) :
  8. data(data)
  9. {}
  10. T* data;
  11.  
  12. bool operator==(const myIterator& rhs){return rhs.data == data;}
  13. bool operator!=(const myIterator& rhs){return rhs.data != data;}
  14. T* operator*(){return data;}
  15. T* operator->(){return data;}
  16.  
  17. myIterator& operator++(){data = &data[1]; return *this; }
  18. };
  19.  
  20. int main() {
  21.  
  22. int myData[] = {
  23. 1, 2, 3,
  24. 4, 5, 6,
  25. 7, 8, 9
  26. };
  27.  
  28. size_t w = 3, h = 3;
  29. std::vector<std::vector<int*>> data;
  30. data.reserve(w);
  31.  
  32. for (size_t i = 0; i < w; ++i)
  33. {
  34. data.emplace_back(myIterator<int>(&myData[i * h]),
  35. myIterator<int>(&myData[(i + 1) * h]));
  36. }
  37.  
  38. for (const auto& i : data)
  39. for (const auto& j : i)
  40. std::cout << *j << std::endl;
  41.  
  42. std::cout << "or with index access: " << std::endl;
  43.  
  44. for (size_t i = 0; i < w; ++i)
  45. for (size_t j = 0; j < h; ++j)
  46. std::cout << *data[i][j] << std::endl;
  47. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
1
2
3
4
5
6
7
8
9
or with index access: 
1
2
3
4
5
6
7
8
9