#include <vector>
#include <iostream>
#include <memory>
template<typename T, size_t nDimensions>
class MArray;
template<typename T, size_t nDimensions>
class MArrayProxy : public MArrayProxy<T, nDimensions - 1>
{
public:
MArrayProxy(MArray<T, nDimensions> &arr)
: MArrayProxy<T, nDimensions - 1>(arr)
{
}
MArrayProxy<T, nDimensions - 1> &operator[](size_t idx)
{
this->position += idx * reinterpret_cast<MArray<T, nDimensions>&>(this->arr).elementSize;
return *this;
}
};
template<typename T>
class MArrayProxy<T, 1>
{
public:
MArrayProxy(MArray<T, 1> &arr)
: position(0), arr(arr)
{
}
T &operator[](size_t idx)
{
position += idx;
return arr[position];
}
protected:
size_t position;
MArray<T, 1> &arr;
};
template<typename T, size_t nDimensions>
class MArray : public MArray<T, nDimensions - 1>
{
public:
template<typename... SizeTypes>
MArray(typename std::enable_if<sizeof...(SizeTypes) == nDimensions - 2, size_t>::type sz, size_t sz2, SizeTypes... sizes)
: MArray<T, nDimensions - 1>(sz * sz2, sizes...)
, elementSize(sz2 * MArray<T, nDimensions - 1>::elementSize)
{
}
MArrayProxy<T, nDimensions - 1> operator[](size_t idx)
{
return MArrayProxy<T, nDimensions>(*this)[idx];
}
protected:
friend class MArrayProxy<T, nDimensions>;
size_t elementSize;
};
template<typename T>
class MArray<T, 1>
{
public:
MArray(size_t sz)
: data(new T[sz])
{
}
T &operator[](size_t idx)
{
return data[idx];
}
protected:
std::unique_ptr<T[]> data;
const size_t elementSize = 1;
};
int main()
{
int n, m;
std::cin >> n >> m;
MArray<int, 2> matrix(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
std::cin >> matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKdGVtcGxhdGU8dHlwZW5hbWUgVCwgc2l6ZV90IG5E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