ping@cubmol.BIO.COLUMBIA.EDU (Shiping Zhang) (09/05/89)
The following is a one-dimensional vector (very simple)
class intv {
int sz;
int *v;
public:
intv();
~intv();
void set_size(int);
int& operator[](int);
};
intv::~intv()
{
delete v;
}
intv::intv()
{
sz=0;
v = NULL;
}
void intv::set_size(int len)
{
int *t=new int[len];
for(long i=0;i<sz;i++){t[i]=v[i];}
delete v;
v=t;
sz=len;
}
int& intv::operator[](int i)
{
if(i<0){cerr << "Negative index of vector\n";exit(0);}
if(i>=sz) {
set_size(sz+LENGTH); // LENGTH is a constant
}
return v[i];
}
// an example of using intv
#define LENGTH 5;
#include <stream.h>
#include "intv.h"
main()
{
intv a1;
for(int i=0;i<10;i++)
a1[i]=i;
for(i=0;i<20;i++)
cout << a1[i]*2; //a1 can be used without worrying its rang
}
Now the question is if there is a way to define a multi-dimensional
vector which can be used freely without concern about boundry,
for example, as following:
//...
// suppose intmv is defined as a vector, like intv above
intmv a2[4][4]; // now define a 2d array of intmv ???
int i,k;
for(i=0;i<4;i++)
for(k=0;k<4;k++)
a2[i][k][i*4+k]=i*k; // a2 being used freely
// without checking the rang of
// its 3rd dimemsion ???
Is this possible or legal? If yes, how to define intmv?
I appreciate any help about this matter. Thanks.
-ping