#include <vector>
#include <iostream>
using namespace std;
template<class T>
class Array2D {
vector<T> data;
size_t cols;
public:
template <class U>
class Array2DIndexer {
size_t offset;
vector<U> &data;
public:
Array2DIndexer(size_t o, vector<U> &dt) : offset(o), data(dt) {}
T& operator[](size_t index) {
return data[offset+index];
}
};
Array2D(size_t r, size_t c) : data (r*c), cols(c) {}
Array2DIndexer<T> operator[](size_t index) {
return Array2DIndexer<T>(index*cols, data);
}
};
int main() {
Array2D<int> a2d(10, 20);
for (int r = 0 ; r != 10 ; r++) {
for (int c = 0 ; c != 20 ; c++) {
a2d[r][c] = r+2*c+1;
}
}
for (int r = 0 ; r != 10 ; r++) {
for (int c = 0 ; c != 20 ; c++) {
cout << a2d[r][c] << " ";
}
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGU8Y2xhc3MgVD4KY2xhc3MgQXJyYXkyRCB7Cgl2ZWN0b3I8VD4gZGF0YTsKCXNpemVfdCBjb2xzOwpwdWJsaWM6CiAgICB0ZW1wbGF0ZSA8Y2xhc3MgVT4KICAgIGNsYXNzIEFycmF5MkRJbmRleGVyIHsKICAgIAlzaXplX3Qgb2Zmc2V0OwogICAgCXZlY3RvcjxVPiAmZGF0YTsKICAgIHB1YmxpYzoKICAgICAgICBBcnJheTJESW5kZXhlcihzaXplX3QgbywgdmVjdG9yPFU+ICZkdCkgOiBvZmZzZXQobyksIGRhdGEoZHQpIHt9CiAgICAgICAgVCYgb3BlcmF0b3JbXShzaXplX3QgaW5kZXgpIHsKICAgICAgICAJcmV0dXJuIGRhdGFbb2Zmc2V0K2luZGV4XTsKICAgICAgICB9CiAgICB9OwogICAgQXJyYXkyRChzaXplX3Qgciwgc2l6ZV90IGMpIDogZGF0YSAocipjKSwgY29scyhjKSB7fQogICAgQXJyYXkyREluZGV4ZXI8VD4gb3BlcmF0b3JbXShzaXplX3Qga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