#include <iostream>
#include <vector>
template<typename T>
struct myIterator : public std::iterator<std::input_iterator_tag, T*>
{
myIterator(T* data) :
data(data)
{}
T* data;
bool operator==(const myIterator& rhs){return rhs.data == data;}
bool operator!=(const myIterator& rhs){return rhs.data != data;}
T* operator*(){return data;}
T* operator->(){return data;}
myIterator& operator++(){data = &data[1]; return *this; }
};
int main() {
int myData[] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
size_t w = 3, h = 3;
std::vector<std::vector<int*>> data;
data.reserve(w);
for (size_t i = 0; i < w; ++i)
{
data.emplace_back(myIterator<int>(&myData[i * h]),
myIterator<int>(&myData[(i + 1) * h]));
}
for (const auto& i : data)
for (const auto& j : i)
std::cout << *j << std::endl;
std::cout << "or with index access: " << std::endl;
for (size_t i = 0; i < w; ++i)
for (size_t j = 0; j < h; ++j)
std::cout << *data[i][j] << std::endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdGVtcGxhdGU8dHlwZW5hbWUgVD4Kc3RydWN0IG15SXRlcmF0b3IgOiBwdWJsaWMgc3RkOjppdGVyYXRvcjxzdGQ6OmlucHV0X2l0ZXJhdG9yX3RhZywgVCo+CnsKICAgIG15SXRlcmF0b3IoVCogZGF0YSkgOgogICAgICBkYXRhKGRhdGEpCiAgICB7fQogICAgVCogZGF0YTsKCiAgICBib29sIG9wZXJhdG9yPT0oY29uc3QgbXlJdGVyYXRvciYgcmhzKXtyZXR1cm4gcmhzLmRhdGEgPT0gZGF0YTt9CiAgICBib29sIG9wZXJhdG9yIT0oY29uc3QgbXlJdGVyYXRvciYgcmhzKXtyZXR1cm4gcmhzLmRhdGEgIT0gZGF0YTt9CiAgICBUKiBvcGVyYXRvciooKXtyZXR1cm4gZGF0YTt9CiAgICBUKiBvcGVyYXRvci0+KCl7cmV0dXJuIGRhdGE7fQoKICAgIG15SXRlcmF0b3ImIG9wZXJhdG9yKysoKXtkYXRhID0gJmRhdGFbMV07IHJldHVybiAqdG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