Array模板类

模板类算是比较麻烦的一个东西,在这儿我记一个我自己写的模板Array类:

 

#include<iostream>
#include<cstring>
#include"Array.h"
#include"outOfRange.h"
using namespace std;
template <class Template>
class Array
{
    friend ostream& operator<<(ostream& out,const Array<Template>& temp)
    {
           out<<"Array[ ";
           //int x = sizeof(temp)/sizeof(int);
           //cout<<temp.getMaxLength()<<endl;
           for(int i = 0;i<temp.getMaxLength();i++)
            {
                out<<temp[i]<<" ";
            }
            out<<"]"<<endl;
            return out;
    }
    friend istream& operator>>(istream& iso,Array<Template>& temp)
    {
            //int x = sizeof(temp)/sizeof(int);
            //cout<<temp.getMaxLength()<<endl;
            //cout<<&temp<<endl;
            if(temp.getMaxLength()==0)
            {
                cout<<"You don't create any space for you."<<endl;
                cout<<"Please input you want create space's size:"<<endl;
                int size;
                cin>>size;
                Array<Template> tmp(size);
                temp = tmp;
                //temp = new Template[size];
                cin.sync();
            }
            cout<<"Please input the value ( "<<temp.getMaxLength()<<
                (temp.getMaxLength()>1?" values ) :":" value ) : ")<<endl;
            for(int i = 0;i<temp.getMaxLength();i++)
            {
                //cout<<i<<endl;
                //cout<<&temp[i]<<endl;
                iso>>temp[i];
            }

            //cout<<&temp<<endl;

            iso.sync();
            return iso;
    }
    public:
        Array(int _maxIndex = 0);
        Array(const Array<Template>&);
        virtual ~Array();
        Template& operator[](const int index);
        Template& operator[](const int index) const;
        Array<Template>& operator=(const Array<Template>& source);
        int getMaxLength()const {return maxIndex;}
        void setArray(const Array<Template>& source);

    private:
        Template* temp;
        int maxIndex;
};

template <class Template>
Array<Template>::Array(int _maxIndex)
{
    maxIndex = _maxIndex;
    temp = new Template[maxIndex];
}
template <class Template>
Array<Template>::~Array()
{
    //cout<<"\nFree!!"<<endl;
    delete [] temp;
}
template <class Template>
Template& Array<Template>::operator[](const int index)
{
     if(index>=this->getMaxLength()){
            throw outOfRange(this->getMaxLength(),index);
     }
    return temp[index];
}
template <class Template>
Template& Array<Template>::operator[](const int index)const
{
    if(index>=this->getMaxLength()){
            throw outOfRange(this->getMaxLength(),index);
     }
    return temp[index];
}
template <class Template>
Array<Template>::Array(const Array<Template>& source)
{
    setArray(source);
}

template <class Template>
Array<Template>& Array<Template>::operator=(const Array<Template>& source)
{
    setArray(source);
    return *this;
}
template <class Template>
void Array<Template>::setArray(const Array<Template>& source)
{
    maxIndex = source.getMaxLength();
    temp = new Template[maxIndex];

    for(int i = 0;i<maxIndex;++i)
    {
        temp[i] = source[i];
    }
}

int main(int argc,char** argv)
{
    try{
        Array<int> intarray(3);

        cin>>intarray;
        cout<<intarray;

        Array<string> stringtarray(3);

        cin>>stringtarray;
        cout<<stringtarray;
        cout<<stringtarray[5];
        Array<string> stringtarray1(stringtarray);
        cout<<stringtarray1;
        Array<int> intarray2;
        cin>>intarray2;
        cout<<intarray2;
    }catch(outOfRange e)
    {
        cout<<e.what()<<endl;
    }


    return 0;
}


所有的代码组织在一个CPP文件中,因为分离的话,会出现各种莫名奇妙的问题,虽然说部分能够解决,但是重载的输入输出,一直没有找到资料,所以在即随后把所有的定义与实现放在了一个cpp文件里面。这里面有数组越界的异常处理,是自己写的,参考代码如下:

 

outOfRange.h

 

#ifndef OUTOFRANGE_H
#define OUTOFRANGE_H
#include<exception>
using namespace std;
class outOfRange:public exception
{
    public:
        outOfRange( int _maxIndex=0,int _outOfRangeIndex=0);
        virtual ~outOfRange() throw();
        const char* what();
    private:
        int maxIndex;
        int outOfRangeIndex;
        const char* message;
};

#endif // OUTOFRANGE_H


 

 

outOfRange.cpp

 

#include "outOfRange.h"
#include<iostream>
#include<cstdio>
using namespace std;
outOfRange::outOfRange(int _maxIndex,int _outOfRangeIndex)
{
    maxIndex = _maxIndex;
    outOfRangeIndex = _outOfRangeIndex;
}

outOfRange::~outOfRange() throw()
{
}
const char* outOfRange::what()
{
    //stringstream os;
    //message = new char[100];
    //cout.write()
    message = new char[100];
    sprintf(const_cast<char*>(message),"Out of range %d , Must between 1 and %d",outOfRangeIndex+1,maxIndex);
    //cout<<outOfRangeIndex<<" "<<maxIndex<<endl;
    //cout<<message<<endl;
    //os<<<<endl;
    return message;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值