CSDN博客

img owl2008

【头文件】c++实现数组

发表于2004/7/15 14:35:00  1550人阅读

分类: 源代码

#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

0 0

相关博文

我的热门文章

img
取 消
img