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

这是一个C++实现的动态数组类模板,包含构造函数、赋值运算符、大小查询、最大值和最小值方法。类中使用了运算符重载和私有初始化函数来管理数组元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值