模板实现任意维数组

 

在OOT课程中老师给出了模板实现的2维数组,我受到启发写了一个实现任意维数组的模板。它与C++中默认的模板很像,并且能够检查越界。

// MyArray.cpp
// by jefferyzb @ 07.01.14
// OOT 2001_No.3


#include 
< iostream.h >
#include 
< stdarg.h >

// original 2-demission array template

template 
< class  T >   class  Array;

template 
< class  T >   class  ArrayTmp  // 第二层类型
    friend class Array<T>;
    T
* tpBody;
    
int iRows, iColumns, iCurrentRow;
    
    ArrayTmp(
int iRsz, int iCsz)
    

        tpBody 
= new T[iRsz*iCsz]; 
        iRows 
= iRsz; 
        iColumns 
= iCsz;
        iCurrentRow 
= -1
    }

    
public:
    T
& operator[ ](int j) 
    

        
return tpBody[iCurrentRow + j * iRows]; 
    }

}
;

template 
< class  T >   class  Array  // 第一层类型
    ArrayTmp<T> tTmp; // 第一层类型与第二层类型的关联
    
public:
    Array(
int iRsz, int iCsz) : tTmp(iRsz, iCsz) {}
    
    ArrayTmp
<T>& operator[ ](int i)
    

        tTmp.iCurrentRow 
= i; 
        
return tTmp; 
    }

}
;
///
// modified n-demission array template

int  cnt  =   1 ;

class  Integer
{
    
public:
    
int n;

    Integer(
int i=cnt) 
    
{
        n 
= i;
        cnt
++;
    }


    inline friend ostream 
& operator << (ostream& o, const Integer& output)
    
{
        o 
<<"Integer:"<<output.n;
        
return o;
    }

}
;

template 
< class  T >   class  ArrayBase 

    T
* tpBody;
    
int demision,curDemision;
    
int * ipL;
    
int length;
    
int index;

public:

    ArrayBase(
int demision, ...)
    

        
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++ )
        
{
            iL 
= va_arg (ap, int);
            
this->ipL[i] = iL;
            length 
*= iL;
        }

        va_end (ap);

        
this->tpBody = new T[length];
    }

    
    
    ArrayBase
& operator[ ](int j) 
    

        
//refresh when reach the last brackets
        if(++curDemision > demision)
        
{
            
this->curDemision = 1;
            
this->index = 0;
        }


        
int size = 1;
        
        
for (int i=demision-1; i>curDemision-1; i-- )
        
{
            size 
*= this->ipL[i];
        }


        
this->index += j * size;
    
        
//check
        if(this->index >= this->length)
        
{
            cout
<<"Array out of bound ";
            
this->index = 0;
        }


        
return *this
    }

    
    ArrayBase 
& operator=(T t)
    
{
        
*(tpBody+index) = t;
        
return *this;
    }

    
    friend ostream 
& operator << (ostream& o, const ArrayBase& output)
    
{
        T 
* t = output.tpBody;
        t 
+= output.index;
        o
<<*t;
        
return o;
    }

}
;



void  main()
{
    ArrayBase
<int> a(4,2,3,4,5);
    a[
1][2][3][4= 9;
    cout
<<a[1][2][3][4]<<endl;

    ArrayBase
<Integer> b(4,2,3,4,5);
    b[
1][2][3][4= Integer(11);
    cout
<<b[1][2][3][4]<<endl;
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值