在OOT课程中老师给出了模板实现的2维数组,我受到启发写了一个实现任意维数组的模板。它与C++中默认的模板很像,并且能够检查越界。
//
MyArray.cpp
//
by jefferyzb @ 07.01.14
//
OOT 2001_No.3
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#include
<
iostream.h
>
#include
<
stdarg.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
original 2-demission array template
template
<
class
T
>
class
Array;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
template
<
class
T
>
class
ArrayTmp
...
{ // 第二层类型
friend class Array<T>;
T* tpBody;
int iRows, iColumns, iCurrentRow;
ArrayTmp(int iRsz, int iCsz)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tpBody = new T[iRsz*iCsz];
iRows = iRsz;
iColumns = iCsz;
iCurrentRow = -1;
}
public:
T& operator[ ](int j)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return tpBody[iCurrentRow + j * iRows];
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
template
<
class
T
>
class
Array
...
{ // 第一层类型
ArrayTmp<T> tTmp; // 第一层类型与第二层类型的关联
public:
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Array(int iRsz, int iCsz) : tTmp(iRsz, iCsz) ...{}
ArrayTmp<T>& operator[ ](int i)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tTmp.iCurrentRow = i;
return tTmp;
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
///
//
modified n-demission array template
int
cnt
=
1
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
class
Integer
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public:
int n;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Integer(int i=cnt)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
n = i;
cnt++;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
inline friend ostream & operator << (ostream& o, const Integer& output)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
o <<"Integer:"<<output.n;
return o;
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
T
>
class
ArrayBase
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
T* tpBody;
int demision,curDemision;
int * ipL;
int length;
int index;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public:
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ArrayBase(int demision, ...)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this->demision = demision;
this->curDemision = 0;
this->ipL = new int[demision];
this->length = 1;
this->index = 0;
int iL;
va_list ap;
va_start ( ap, demision );
for ( int i= 0; i< demision; i++ )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
iL = va_arg (ap, int);
this->ipL[i] = iL;
length *= iL;
}
va_end (ap);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
this->tpBody = new T[length];
}
ArrayBase& operator[ ](int j)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//refresh when reach the last brackets
if(++curDemision > demision)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this->curDemision = 1;
this->index = 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
int size = 1;
for (int i=demision-1; i>curDemision-1; i-- )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
size *= this->ipL[i];
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
this->index += j * size;
//check
if(this->index >= this->length)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<"Array out of bound ";
this->index = 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return *this;
}
ArrayBase & operator=(T t)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
*(tpBody+index) = t;
return *this;
}
friend ostream & operator << (ostream& o, const ArrayBase& output)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
T * t = output.tpBody;
t += output.index;
o<<*t;
return o;
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
main()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
ArrayBase<int> a(4,2,3,4,5);
a[1][2][3][4] = 9;
cout<<a[1][2][3][4]<<endl;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ArrayBase<Integer> b(4,2,3,4,5);
b[1][2][3][4] = Integer(11);
cout<<b[1][2][3][4]<<endl;
}