#include <vector>
#include <cstdlib>
#include <iostream>
template <typename T>
class Matrix_Proxy
{
public:
Matrix_Proxy(std::vector<T>& _ref, size_t _size) : ref(_ref), size(_size)
{}
T& operator[](int i)
{
return ref[i];
}
const T& operator[](int i) const
{
return ref[i];
}
private:
std::vector<T>& ref;
size_t size;
};
template <typename T>
class Matrix_Proxy_Const
{
public:
Matrix_Proxy_Const(const std::vector<T>& _ref, size_t _size) : ref(_ref), size(_size)
{}
const T& operator[](int i) const
{
return ref[i];
}
private:
const std::vector<T>& ref;
size_t size;
};
template <typename T>
class Matrix
{
public:
Matrix(size_t x) : values(x), size(x)
{
for(auto&& y : values)
{
y.resize(x);
for(auto&& x : y)
x = 0;
}
}
Matrix_Proxy<T> operator [] (int i)
{
return Matrix_Proxy<T>(values[i],size);
}
const Matrix_Proxy_Const<T> operator [] (int i) const
{
return Matrix_Proxy_Const<T>(values[i],size);
}
private:
std::vector<std::vector<T>> values;
size_t size;
};
int main()
{
Matrix<int> intMat(5); //FINE
std::cout << intMat[2][2] << std::endl; //FINE
const Matrix<int> cintMat(5); //FINE
std::cout << cintMat[2][2] << std::endl; //FINE
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCnRlbXBsYXRlIDx0eXBlbmFtZSBUPgpjbGFzcyBNYXRyaXhfUHJveHkKewpwdWJsaWM6CiAgICBNYXRyaXhfUHJveHkoc3RkOjp2ZWN0b3I8VD4mIF9yZWYsIHNpemVfdCBfc2l6ZSkgOiByZWYoX3JlZiksIHNpemUoX3NpemUpCiAgICB7fQogICAgVCYgb3BlcmF0b3JbXShpbnQgaSkKICAgIHsKICAgICAgICByZXR1cm4gcmVmW2ldOwogICAgfQoKICAgIGNvbnN0IFQmIG9wZXJhdG9yW10oaW50IGkpIGNvbnN0CiAgICB7CiAgICAgICAgcmV0dXJuIHJlZltpXTsKICAgIH0KCnByaXZhdGU6CiAgICBzdGQ6OnZl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