PA2-zkouska/m.cpp

64 lines
1.4 KiB
C++

template<class T, unsigned ... RestDim> struct Matrix;
template<class T, unsigned FirstDim >
struct Matrix<T, FirstDim> {
typedef T GenericDimType[FirstDim];
GenericDimType data;
inline T& operator[](unsigned i) { return data[i]; }
};
template<class T, unsigned FirstDim, unsigned ... RestDim >
struct Matrix<T, FirstDim, RestDim...> {
typedef typename Matrix<T, RestDim...>::GenericDimType FirstDimType;
typedef FirstDimType GenericDimType[FirstDim];
GenericDimType data;
inline FirstDimType& operator[](unsigned i) { return data[i]; }
};
#include <iostream>
int main() {
Matrix<int, 2, 3, 4> m;
int cnt = 0;
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t h = 0; h < 4; ++h) {
m[i][j][h] = cnt++;
}
}
}
std::cout << "Data in matrix:\n";
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t h = 0; h < 4; ++h) {
std::cout << m[i][j][h] << ' ';
}
std::cout << '\n';
}
std::cout << "ENDDIM\n";
}
std::cout << "Addresses in matrix:\n";
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t h = 0; h < 4; ++h) {
std::cout << &m[i][j][h] << ' ';
}
std::cout << '\n';
}
std::cout << "ENDDIM\n";
}
return 0;
}