1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
class Array3D {
public:
Array3D(int I, int J, int K);
vector<double> data;
double &operator()(int I, int J, int K);
int i, j, k;
};
Array3D::Array3D(int I, int J, int K) :
i(I), j(J), k(K), data(I*J*K)
{;}
double &
Array3D::operator()(int I, int J, int K)
{
if (I<0 || I >= i) {
cerr << "i out of bounds\n";
abort();
}
if (J<0 || J >= j) {
cerr << "j out of bounds\n";
abort();
}
if (K<0 || K >= k) {
cerr << "k out of bounds\n";
abort();
}
return data[I*j*k + J*k + K];
}
int main(int argc, char *argv[])
{
Array3D arr(3,4,5);
int i,j,k;
int x=0;
for (i=0; i<3; ++i) {
for (j=0; j<4; ++j) {
for (k=0; k<5; ++k) {
arr(i,j,k) = ++x;
}
}
}
for (i=0; i<3; ++i) {
for (j=0; j<4; ++j) {
for (k=0; k<5; ++k) {
cout << arr(i,j,k) << ' ';
}
cout << '\n';
}
}
return 0;
}
|