模板类算是比较麻烦的一个东西,在这儿我记一个我自己写的模板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;
}