模板實現任意維數組

 

在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;
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章