【標頭檔】c++實現數組

來源:互聯網
上載者:User

#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using namespace std; 
template<typename elemtype> class array {
public:
    //建構函式集合
    array ( int sz = MIN );
    array ( const array<elemtype>& );
    array( const elemtype*,const int );
    //運算子多載集合
    const bool operator ==( const array<elemtype>& ) const;
    const bool operator !=( const array<elemtype>& ) const;
    const array operator =( const array<elemtype>& );
    elemtype &operator [] ( const int index );

    //內建方法
    const int size() const ;
    const elemtype min() const ;
    const elemtype max() const ;

private:
    //私人函數
    void array_init( const elemtype *ap,const int sz );//初始化
   
 //私人資料
    elemtype     *ia;//數組頭指標
    int         _size;//數組長度
};

//數組實現代碼
//私人函數
template<typename elemtype>
    void
    array<elemtype>::array_init(const elemtype *ap,
                  const int sz)
    {
       _size = sz;
       ia = new elemtype [ _size ];
       for( int i = 0;i < _size;++i )
    {
           if( !ap )
              ia[ i ] = 0;
           else
              ia[ i ] = ap[ i ];
    }

}
//公有函數
//建構函式集合
template<typename elemtype>
    array<elemtype>::array ( int sz )
    {
       array_init( 0,sz );
    }
template<typename elemtype>
    array<elemtype>::array ( const array<elemtype> &a)  
    {
       array_init( a.ia,a._size );
    }
template<typename elemtype>
    array<elemtype>::array( const elemtype *ay,
                         const int sz )
{
    array_init( ay,sz );
}

//方法集合
template<typename elemtype>
    const elemtype array<elemtype>::max() const  
    {
       elemtype *max = ia;
       for( int ix = 0; ix < _size; ++ix )
           max = *max < ia[ ix ]? ia + ix: max;
       return *max;
    }
template<typename elemtype>
    const elemtype array<elemtype>::min() const
    {
       elemtype *min = ia;
       for( int ix = 0; ix < _size; ++ix )
           min = *min > ia[ ix ]? ia + ix: min;
       return *min;
    }
template<typename elemtype>
    const int array<elemtype>::size()  const
    {
       return _size;
    }

//操作符集合
template<typename elemtype>
    elemtype& array<elemtype>::operator []( const int index )
    {
       assert( index >= 0 && index <= _size );
       return ia[ index ];
    }

template<typename elemtype>
    const array<elemtype>
    array<elemtype>::operator =( const array<elemtype> &a )
    {
           _size = a._size;
           ia = new elemtype [ _size ];
           for( int i = 0;i <= a._size;++i )
           ia[ i ] = a.ia[ i ];

           return *this;
    }

template<typename elemtype>
    const bool
    array<elemtype>::
    operator ==( const array<elemtype> &A ) const
    {
       bool x = true;
       try
    {
     if( _size != A._size )
              throw x;
           for( int i = 0; i <= _size; ++i )
              if( ia[ i ] != A.ia[ i ] )
                  throw x;
       }
       catch( bool x )
    {
           return x = false;
       }

       return x;
    }

template<typename elemtype>
    const bool
    array<elemtype>::
    operator !=( const array<elemtype> &A )  const
    {
       bool x = true;
       try
    {
           if( _size == A._size )
              throw x;
           for( int i = 0; i <= _size; ++i)
              if( ia[ i ] == A.ia[ i ] )
                  throw x;
       }
       catch( bool x )
    {
           return x = false;
       }

       return x;
    }

#endif

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.