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 55 56 57
|
#include <iostream> //Required for cin, cout
using std::cin;
using std::cout;
class Arr3D {
public:
Arr3D(unsigned x, unsigned y, unsigned z);
~Arr3D();
double &operator()(unsigned x, unsigned y, unsigned z);
private:
Arr3D(const Arr3D&);
double &operator=(const Arr3D&);
unsigned nx, ny, nz;
double *data;
};
Arr3D::Arr3D(unsigned x, unsigned y, unsigned z) :
nx(x), ny(y), nz(z), data(new double[x*y*z])
{}
Arr3D::~Arr3D()
{
delete[] data;
}
double &
Arr3D::operator()(unsigned x, unsigned y, unsigned z)
{
return data[z + y*nz + x*ny*nz];
}
int
main(int argc, char **argv)
{
unsigned nx, ny, nz;
cin >> nx >> ny >> nz;
Arr3D myarr(nx, ny, nz);
int i=0;
for (unsigned x=0; x<nx; ++x) {
for (unsigned y=0; y<ny; ++y) {
for (unsigned z=0; z<nz; ++z) {
myarr(x,y,z) = ++i;
}
}
}
for (unsigned x=0; x<nx; ++x) {
for (unsigned y=0; y<ny; ++y) {
for (unsigned z=0; z<nz; ++z) {
cout << myarr(x,y,z) << ' ';
}
cout << '\n';
}
cout << '\n';
}
}
| |