0. 前言
“那天,我脑子一热……” “然后呢?” “研究了一点点模板库……” “C++ 标准的模板库不是挺好的吗?” “是啊,但是,我挺讨厌他们的命名方式的……”
1. 简介
C++ 的标准模板库,也就是常说的 “STL”。虽然都是专家编写的,但是其中有一些我个人并不是很……的。于是,我就自研究其模板库来了。 先贴上一份写了三个月的代码,自取,可以直接食用。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<stack>
#define Pass
using namespace std;
const int NullChar=-1;const int TimesStep=668002672;
template<typename Type>
void Fill(Type *Array,const int Len,const Type Value)
{
Type *Pointer=Array;
for(int Index=0;Index<Len;Index++)
{
*Pointer=Value;
Pointer++;
}
}
template<typename Type,typename Functor>
void Fill(Type *Array,const int Len,Functor Function)
{
Type *Pointer=Array;
for(int Index=0;Index<Len;Index++)
{
*Pointer=Function(Index);
Pointer++;
}
}
template<typename Type>
void Swap(Type &First,Type &Second)
{
Type Temporary;
Temporary=First;
First=Second;
Second=Temporary;
}
template<typename Type>
Type Min(Type Value)
{
return Value;
}
template<typename Type,typename... Args>
Type Min(Type First,Args... Other)
{
return First<Min(Other...)?First:Min(Other...);
}
template<typename Type>
Type Max(Type Value)
{
return Value;
}
template<typename Type,typename... Args>
Type Max(Type First,Args... Other)
{
return First>Max(Other...)?First:Max(Other...);
}
template<typename Type>
void Sort(Type *Array,int Left,int Right)
{
int Middle=Array[(Left+Right)/2];
int InnerIndex=Left,OuterIndex=Right;
do
{
while(Array[InnerIndex]<Middle)
{
InnerIndex++;
}
while(Array[OuterIndex]>Middle)
{
OuterIndex--;
}
if(InnerIndex<=OuterIndex)
{
Swap(Array[InnerIndex],Array[OuterIndex]);
InnerIndex++;
OuterIndex--;
}
}while(InnerIndex<=OuterIndex);
if(Left<OuterIndex)
{
Sort(Array,Left,OuterIndex);
}
if(InnerIndex<Right)
{
Sort(Array,InnerIndex,Right);
}
}
template<typename Type>
void Sort(Type *Array,int Left,int Right,bool Compare)
{
int Middle=Array[(Left+Right)/2];
int InnerIndex=Left,OuterIndex=Right;
do
{
while(Compare(Array[InnerIndex],Middle))
{
InnerIndex++;
}
while(Compare(Array[OuterIndex],Middle))
{
OuterIndex--;
}
if(InnerIndex<=OuterIndex)
{
Swap(Array[InnerIndex],Array[OuterIndex]);
InnerIndex++;
OuterIndex--;
}
}while(InnerIndex<=OuterIndex);
if(Left<OuterIndex)
{
Sort(Array,Left,OuterIndex);
}
if(InnerIndex<Right)
{
Sort(Array,InnerIndex,Right);
}
}
template<typename Type>
Type &&PerfectRefrence(Type &&Value)
{
return static_cast<Type &&>(Value);
}
int StrLen(const char *String)
{
char *Temporary=const_cast<char *>(String);
int Len=0;
while(Temporary[Len]!='\0')
{
Len++;
}
return Len;
}
void None()
{
Pass;
}
void Wait(const double Seconds)
{
for(double Times=0;Seconds*TimesStep-Times*Seconds>=1e8;Times+=1.0)
{
Pass;
}
}
class Exception
{
private:
virtual void Absolute()
{
Pass;
}
public:
char *Message;
int ErrorCode;
int Grade;
Exception()
{
Pass;
}
~Exception()
{
Pass;
}
};
class BaseStream
{
protected:
class StreamError:public Exception
{
public:
void operator ()(const char *Message,const int Code=0,const int Grade=0)
{
(*this).ErrorCode=Code;
(*this).Grade=Grade;
(*this).Message=const_cast<char *>(Message);
}
};
private:
unsigned Configure;
public:
void Set(const unsigned Value)
{
(*this).Configure=Value;
}
};
class InputStream:public BaseStream
{
private:
bool Station;
public:
InputStream()
{
Station=true;
}
~InputStream()
{
Pass;
}
inline InputStream &operator >>(int &Variable)
{
int Number=0,Temp=1;
char Char=getchar();
while(Char<'0'||Char>'9')
{
if(Char=='-')
{
Temp=-1;
Char=getchar();
}
}
while(Char>='0'&&Char<='9')
{
Number=Number*10+Char-'0';
Char=getchar();
}
Variable=Number*Temp;
return (*this);
}
inline InputStream &operator >>(long &Variable)
{
long Number=0,Temp=1;
char Char=getchar();
while(Char<'0'||Char>'9')
{
if(Char=='-')
{
Temp=-1;
Char=getchar();
}
}
while(Char>='0'&&Char<='9')
{
Number=Number*10+Char-'0';
Char=getchar();
}
Variable=Number*Temp;
return (*this);
}
inline InputStream &operator >>(short &Variable)
{
short Number=0,Temp=1;
char Char=getchar();
while(Char<'0'||Char>'9')
{
if(Char=='-')
{
Temp=-1;
Char=getchar();
}
}
while(Char>='0'&&Char<='9')
{
Number=Number*10+Char-'0';
Char=getchar();
}
Variable=Number*Temp;
return (*this);
}
inline InputStream &operator >>(char &Variable)
{
Variable=getchar();
return (*this);
}
inline InputStream &operator >>(double &Variable)
{
scanf("%lf",&Variable);
return (*this);
}
inline InputStream &operator >>(float &Variable)
{
scanf("%f",&Variable);
return (*this);
}
inline InputStream &operator >>(char *Variable)
{
scanf("%s",Variable);
return (*this);
}
template<typename Type,typename... Args>
void operator ()(Type &&This,Args&&... Other)
{
(*this)>>PerfectRefrence(This);
(*this)(PerfectRefrence(Other)...);
}
void operator ()()
{
Pass;
}
}In;
class OutputStream:public BaseStream
{
private:
bool Station;
public:
OutputStream()
{
Station=true;
}
~OutputStream()
{
Pass;
}
inline OutputStream &operator <<(const int Variable)
{
int Value=Variable;
if(Value<0)
{
putchar('-');
Value=-Value;
}
if(Value>9)
{
(*this)<<(Value/10);
}
putchar(Value%10+'0');
return (*this);
}
inline OutputStream &operator <<(const char Variable)
{
if(Variable!=NullChar)
{
putchar(Variable);
}
return (*this);
}
inline OutputStream &operator <<(const double &Variable)
{
scanf("%lf",&Variable);
return (*this);
}
inline OutputStream &operator <<(const float &Variable)
{
printf("%f",&Variable);
return (*this);
}
inline OutputStream &operator <<(const char *Variable)
{
printf(Variable);
return (*this);
}
template<typename Type,typename... Args>
void operator ()(Type &&This,Args&&... Other)
{
(*this)<<PerfectRefrence(This);
(*this)(PerfectRefrence(Other)...);
}
void operator ()()
{
Pass;
}
}Out;
class ErrorStream:public BaseStream
{
private:
bool Station;
public:
ErrorStream()
{
Station=true;
}
~ErrorStream()
{
Pass;
}
inline ErrorStream &operator <<(const int Variable)
{
int Value=Variable;
if(Value<0)
{
putchar('-');
Value=-Value;
}
if(Value>9)
{
(*this)<<(Value/10);
}
putchar(Value%10+'0');
return (*this);
}
inline ErrorStream &operator <<(const char Variable)
{
if(Variable!=NullChar)
{
putchar(Variable);
}
return (*this);
}
inline ErrorStream &operator <<(const double &Variable)
{
scanf("%lf",&Variable);
return (*this);
}
inline ErrorStream &operator <<(const float &Variable)
{
printf("%f",&Variable);
return (*this);
}
inline ErrorStream &operator <<(const char *Variable)
{
printf(Variable);
return (*this);
}
}Err;
void Exit(const int ReturnCode)
{
exit(ReturnCode);
}
void Raise(const Exception &ErrorObject,bool RealExit=false)
{
Err<<"[ErrorCode:"<<ErrorObject.ErrorCode<<",Grade-"<<ErrorObject.Grade<<"]"<<ErrorObject.Message;
if(RealExit==true)
{
Exit(0);
}
}
class Numbers
{
private:
bool Sign;
int Array[10001];
int Len;
class NumbersError:public Exception
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=1;
}
};
class DividedZeroError:public NumbersError
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=2;
}
};
class BadNumbersObjectError:public NumbersError
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=1;
}
};
public:
Numbers()
{
Sign=true;
Fill(Array,10000,0);
Len=1;
}
Numbers(const int Value)
{
Fill(Array,10000,0);
(*this)=Value;
}
bool operator <(const Numbers Other)
{
Numbers This=(*this);
if(This.Sign!=Other.Sign)
{
return!This.Sign;
}
else
{
if (This.Len!=Other.Len)
{
return (This.Len<Other.Len)^(!This.Sign);
}
else
{
for(int Index=This.Len;Index>=0;Index--)
{
if(This.Array[Index]<Other.Array[Index])
{
return true;
}
else if(This.Array[Index]>Other.Array[Index])
{
return false;
}
}
return false;
}
}
return false;
}
bool operator ==(const Numbers Other)
{
Numbers This=(*this);
if(This.Sign==Other.Sign&&This.Len==Other.Len)
{
for(int Index=This.Len;Index>=1;Index--)
{
if(Other.Array[Index]!=This.Array[Index])
{
return false;
}
}
}
else
{
return false;
}
return true;
}
bool operator >(const Numbers Other)
{
Numbers This=(*this);
if((This<Other)==false&&(This==Other)==false)
{
return true;
}
else
{
return false;
}
}
bool operator >=(const Numbers Target)
{
return (Target==(*this))||((*this)>Target);
}
bool operator <=(const Numbers Target)
{
return !((*this)>Target);
}
void operator =(const int Value)
{
Fill(Array,10000,0);
(*this).Len=0;
(*this).Sign=(Value>=0);
int Temporary=abs(Value);
if(Value==0)
{
(*this).Array[1]=0;
(*this).Len=1;
}
else
{
while(Temporary>0)
{
(*this).Array[++(*this).Len]=Temporary%10;
Temporary/=10;
}
}
}
Numbers operator +(const Numbers Other)
{
Numbers This=(*this);
if(Other.Sign==This.Sign)
{
This.Len=Max(Len,Other.Len)+10;
int Carry=0;
for(int Index=1;Index<=This.Len;Index++)
{
This.Array[Index]=Array[Index]+Other.Array[Index]+Carry;
Carry=This.Array[Index]/10;
This.Array[Index]%=10;
}
This.Array[This.Len]=Carry;
while(This.Array[This.Len]==0&&This.Len>=2)This.Len--;
if(This.Array[This.Len]==0)
{
This.Sign=true;
}
return This;
}
else
{
if(This.Sign==true)
{
return This-Abs(Other);
}
else
{
Numbers Temporary=(Abs(This)-Other);
if(Temporary.Array[Temporary.Len]==0)
{
Temporary=Abs(Temporary);
}
else
{
Temporary=(-Temporary);
}
return Temporary;
}
}
}
Numbers operator -(const Numbers Current)
{
Numbers This=(*this),Other=Current;
if(Other.Sign==This.Sign==true)
{
bool State=true;
if(This<Other)
{
Numbers Temporary;
Temporary=This;
This=Other;
Other=Temporary;
State=false;
}
This.Len=Max(This.Len,Other.Len)+1000;
int Index=1;
while(Index<=This.Len||Index<=Other.Len)
{
if(This.Array[Index]<Other.Array[Index])
{
This.Array[Index]+=10;
This.Array[Index+1]-=1;
}
This.Array[Index]=This.Array[Index]-Other.Array[Index];
Index++;
}
while(This.Array[This.Len]==0&&This.Len>=2)This.Len--;
This.Sign=State?true:false;
if(This.Array[This.Len]==0)
{
This.Sign=true;
}
return This;
}
else
{
if(Other.Sign==false)
{
return This+Abs(Other);
}
else
{
return This+(-Other);
}
}
}
Numbers operator *(const Numbers Other)
{
Numbers This=(*this),Result(0);
Result.Len=This.Len+Other.Len;
short Carry=0;
for(int Index=0;Index<=Result.Len;Index++)
{
Result.Array[Index]=0;
}
for(int InnerIndex=1;InnerIndex<=This.Len;InnerIndex++)
{
Carry=0;
for(int OuterIndex=1;OuterIndex<=Other.Len;OuterIndex++)
{
int Product=This.Array[InnerIndex]*Other.Array[OuterIndex]+Carry+Result.Array[InnerIndex+OuterIndex-1];
Result.Array[InnerIndex+OuterIndex-1]=Product%10;
Carry=Product/10;
}
Result.Array[InnerIndex+Other.Len]+=Carry;
}
while(Result.Len>1&&Result.Array[Result.Len]==0)
{
Result.Len--;
}
Result.Sign=(This.Sign==Other.Sign);
if(Result.Array[Result.Len]==0)
{
Result.Sign=true;
}
return Result;
}
Numbers operator /(const Numbers Other)
{
if(Other==Numbers(0))
{
DividedZeroError Error;
Error("Divided By Zero!",11);
Raise(Error,true);
return Numbers(0);
}
Numbers Dividend=(*this);
Numbers Divisor=Other;
if(Dividend<Divisor)
{
return Numbers(0);
}
Numbers Quotient(0);
Numbers Remainder(0);
Quotient.Sign=(Dividend.Sign==Divisor.Sign);
Dividend.Sign=true;
Divisor.Sign=true;
for(int Index=Dividend.Len;Index>=1;Index--)
{
Remainder=(Remainder<<Numbers(1))+Numbers(Dividend.Array[Index]);
if(Remainder>=Divisor)
{
int Count=0;
while(Remainder>=Divisor)
{
Remainder-=Divisor;
Count++;
}
Quotient.Array[Index]=Count;
}
}
Quotient.Len=Dividend.Len;
while(Quotient.Len>1&&Quotient.Array[Quotient.Len]==0)
{
Quotient.Len--;
}
if(Quotient.Array[Quotient.Len]==0)
{
Quotient.Sign=true;
}
return Quotient;
}
Numbers operator %(const Numbers Other)
{
Numbers This=Other;
Numbers Remainder(0);
Remainder=(*this)-((*this)/Other*Other);
Remainder.Array[Remainder.Len]=0;
while(Remainder.Array[Remainder.Len]==0&&Remainder.Len>=2)
{
Remainder.Len--;
}
if(Remainder.Array[Remainder.Len]==0)
{
Remainder.Sign=true;
}
return Remainder;
}
Numbers operator <<(const Numbers Times)
{
int Shift=Times;
Numbers Current;
if((*this).Len+Shift>10000)
{
return Current;
}
else
{
for(int Index=(*this).Len;Index>=1;Index--)
{
Current.Array[Index+Shift]=(*this).Array[Index];
}
Current.Len+=Shift+100;
for(int Index=1;Index<=Shift;Index++)
{
Current.Array[Index]=0;
}
while(Current.Len>1&&Current.Array[Current.Len]==0)
{
Current.Len--;
}
return Current;
}
}
Numbers operator >>(const Numbers Times)
{
int Shift=Times;
Numbers Current=(*this);
if(Shift>Current.Len)
{
Fill(Current.Array,10000,0);
Current.Len=1;
Current.Sign=true;
return Current;
}
for(int Index=1;Index<=Current.Len-Shift;Index++)
{
Current.Array[Index]=Current.Array[Index+Shift];
}
for(int Index=Current.Len-Shift+1;Index<=Current.Len;Index++)
{
Current.Array[Index]=0;
}
Current.Len-=Shift;
while(Current.Len>1&&Current.Array[Current.Len]==0)
{
Current.Len--;
}
return Current;
}
void operator >>=(const Numbers Times)
{
(*this)=(*this)>>Times;
}
void operator <<=(const Numbers Times)
{
(*this)=(*this)<<Times;
}
Numbers operator +=(const Numbers Other)
{
(*this)=(*this)+Other;
return (*this);
}
Numbers operator -=(const Numbers Other)
{
(*this)=(*this)-Other;
return (*this);
}
Numbers operator *=(const Numbers Other)
{
(*this)=(*this)*Other;
return (*this);
}
Numbers operator /=(const Numbers Other)
{
(*this)=(*this)/Other;
return (*this);
}
Numbers operator %=(const Numbers Other)
{
(*this)=(*this)%Other;
return (*this);
}
Numbers operator ++(int)
{
(*this)+=Numbers(1);
return (*this)-Numbers(1);
}
Numbers operator ++(void)
{
(*this)+=Numbers(1);
return (*this);
}
Numbers operator --(int)
{
(*this)-=Numbers(1);
return (*this)+Numbers(1);
}
Numbers operator --(void)
{
(*this)-=Numbers(1);
return (*this);
}
Numbers operator -(void)
{
(*this).Sign=!(*this).Sign;
return (*this);
}
Numbers operator +(void)
{
(*this).Sign=(*this).Sign;
return *this;
}
operator int()const
{
int Result=0;
for(int Index=(*this).Len;Index>=1;Index--)
{
Result=Result*10+(*this).Array[Index];
}
return Result;
}
friend InputStream &operator >>(InputStream &Stream,Numbers &This)
{
char Temporary[10001];
scanf("%s",Temporary+1);
int Length=StrLen(Temporary+1),Current=1;
This.Len=Length+10;
if(Temporary[1]=='+'||Temporary[1]=='-')
{
This.Sign=(Temporary[1]=='+');
Current++;
This.Len--;
}
for(int Index=Length;Index>=Current;Index--)
{
This.Array[Length-Index+1]=Temporary[Index]-'0';
}
while(This.Array[This.Len]==0&&This.Len>=2)This.Len--;
return Stream;
}
friend OutputStream &operator <<(OutputStream &Stream,const Numbers This)
{
Stream<<(This.Sign!=false?(char)NullChar:'-');
for(int Index=This.Len;Index>=1;Index--)
{
Stream<<This.Array[Index];
}
return Stream;
}
friend Numbers Abs(Numbers This)
{
This.Sign=true;
return This;
}
};
template<typename Type>
class Container
{
protected:
class Node
{
public:
Type Data;
Node *Front;
Node *Back;
Node(const Type Value)
{
Data=Value;
Front=nullptr;
Back=nullptr;
}
};
public:
Container()
{
Pass;
}
~Container()
{
Pass;
}
};
template<typename Type>
class List:Container<Type>
{
using Node=typename Container<Type>::Node;
private:
class ListError:public Exception
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=1;
}
};
class IndexError:public ListError
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=2;
}
};
class EmptyError:public ListError
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=2;
}
};
Node *Head;
Node *Tail;
int Len;
Type Temporary;
public:
List()
{
Head=new Node(Type());
Tail=new Node(Type());
Head->Back=Tail;
Head->Front=nullptr;
Tail->Front=Head;
Tail->Back=nullptr;
Len=0;
}
List(List&& Other)
{
Head=Other.Head;
Tail=Other.Tail;
Len=Other.Len;
Other.Head=nullptr;
Other.Tail=nullptr;
Other.Len=0;
}
List(const List &Other)
{
Head=new Node(Type());
Tail=new Node(Type());
Head->Back=Tail;
Tail->Front=Head;
Len=0;
Node *Current=Other.Head->Back;
while(Current!=Other.Tail)
{
Append(Current->Data);
Current=Current->Back;
}
}
~List()
{
Clear();
delete Head;
delete Tail;
}
List& operator=(const List &Other)
{
if(this==&Other)
{
return *this;
}
Clear();
Node *Current=Other.Head->Back;
while(Current!=Other.Tail)
{
Append(Current->Data);
Current=Current->Back;
}
return *this;
}
List& operator=(List &&Other)
{
if(this==&Other)
{
return *this;
}
Clear();
delete Head;
delete Tail;
Head=Other.Head;
Tail=Other.Tail;
Len=Other.Len;
Other.Head=nullptr;
Other.Tail=nullptr;
Other.Len=0;
return *this;
}
void Append(const Type Value)
{
Node *Element=new Node(Value);
Len++;
Element->Front=Tail->Front;
Tail->Front=Element;
Element->Back=Tail;
Element->Front->Back=Element;
}
void Prepend(const Type Value)
{
Node *Element=new Node(Value);
Len++;
Element->Back=Head->Back;
Head->Back->Front=Element;
Head->Back=Element;
Element->Front=Head;
}
void Insert(const int TargetIndex,const Type Value)
{
if(TargetIndex<0||TargetIndex>Len)
{
IndexError Error;
Error("List Insert Index Out Of Range!",12);
Raise(Error,false);
return;
}
Node *Element=new Node(Value);
Node *Current=Head;
for(int Index=0;Index<TargetIndex;Index++)
{
Current=Current->Back;
}
Element->Front=Current;
Element->Back=Current->Back;
Current->Back->Front=Element;
Current->Back=Element;
Len++;
}
void Remove(const int TargetIndex)
{
if(TargetIndex<0||TargetIndex>=Len)
{
IndexError Error;
Error("List Remove Index Out Of Range!",12);
Raise(Error,false);
return;
}
Node *Current=Head;
for(int Index=0;Index<=TargetIndex;Index++)
{
Current=Current->Back;
}
Current->Front->Back=Current->Back;
Current->Back->Front=Current->Front;
delete Current;
Len--;
}
void Clear()
{
while(Head->Back!=Tail)
{
Node *Temporary=Head->Back;
Head->Back=Temporary->Back;
delete Temporary;
}
Tail->Front=Head;
Len=0;
}
const int Find(const Type &Value)
{
int Index=0;
Node *Current=Head->Back;
while(Current!=Tail)
{
if(Current->Data==Value)
{
return Index;
}
Current=Current->Back;
Index++;
}
return -1;
}
void Reverse()
{
Node *Current=Head;
while(Current)
{
Node *Temp=Current->Back;
Current->Back=Current->Front;
Current->Front=Temp;
Current=Temp;
}
Swap(Head,Tail);
}
Type& Front()
{
if(Empty())
{
EmptyError Error;
Error("List Is Empty!",15);
Raise(Error,false);
return Type();
}
return Head->Back->Data;
}
Type& Back()
{
if(Empty())
{
EmptyError Error;
Error("List Is Empty!",15);
Raise(Error,false);
return Type();
}
return Tail->Front->Data;
}
Type& operator[](const int TargetIndex)
{
if(TargetIndex<0||TargetIndex>=Len)
{
IndexError Error;
Error("List Index Out Of Range!",17);
Raise(Error,true);
}
Node *Current=Head->Back;
for(int Index=0;Index<TargetIndex;Index++)
{
Current=Current->Back;
}
return Current->Data;
}
const Type& operator[](const int TargetIndex)const
{
return const_cast<List*>(this)->operator[](TargetIndex);
}
int Size()
{
return Len;
}
bool Empty()
{
return Len==0;
}
void Fill(const Type Value)
{
Node *Current=Head->Back;
while(Current!=Tail)
{
Current->Data=Value;
Current=Current->Back;
}
}
template<typename Functor>
void Fill(Functor Function)
{
Node *Current=Head->Back;
while(Current!=Tail)
{
Current->Data=Function(Current->Data);
Current=Current->Back;
}
}
template<typename Functor,typename... Args>
void Fill(Functor Function,Args&&... Other)
{
Node *Current=Head->Back;
while(Current!=Tail)
{
Current->Data=Function(Current->Data,PerfectRefrence(Other)...);
Current=Current->Back;
}
}
};
template<typename Type,typename Adapter=List<Type>>
class Queue:Container<Type>
{
using Node=typename Container<Type>::Node;
private:
int Len;
Adapter AdapterContainer;
class QueueError:public Exception
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=1;
}
};
class EmptyError:public QueueError
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=2;
}
};
public:
Queue()
{
Len=0;
}
~Queue()
{
Pass;
}
Type &Top()
{
if(Len==0)
{
EmptyError Error;
Error("Queue Is Empty!",15);
Raise(Error,false);
return;
}
Numbers Temporary=AdapterContainer[0];
return Temporary;
}
bool Empty()
{
return Len==0;
}
int Size()
{
return Len;
}
void Push(const Type Value)
{
AdapterContainer.Append(Value);
Len++;
}
void Pop()
{
if(Len==0)
{
EmptyError Error;
Error("Queue Is Empty!",15);
Raise(Error,false);
return;
}
AdapterContainer.Remove(0);
Len--;
}
};
template<typename Type,typename Adapter=List<Type>>
class Stack:Container<Type>
{
using Node=typename Container<Type>::Node;
private:
Adapter AdapterContainer;
int Len;
class StackError:public Exception
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=1;
}
};
class EmptyError:public StackError
{
public:
void operator ()(const char *Message,const int Code=0)
{
(*this).ErrorCode=Code;
(*this).Message=const_cast<char *>(Message);
(*this).Grade=2;
}
};
public:
Stack()
{
Len=0;
}
~Stack()
{
Pass;
}
Type *Top()
{
if(Len==0)
{
EmptyError Error;
Error("Stack Is Empty!",15);
Raise(Error,false);
return;
}
Type *Temporary=AdapterContainer[0];
return Temporary;
}
bool Empty()
{
return Len==0;
}
int Size()
{
return Len;
}
void Push(const Type Value)
{
AdapterContainer.Prepend(Value);
Len++;
}
void Pop()
{
if(Len==0)
{
EmptyError Error;
Error("Stack Is Empty!",15);
Raise(Error,false);
return;
}
AdapterContainer.Remove(0);
Len--;
}
};当然,这个模板库不一定会比模板库强,而且这个模板库是我问了几次 DeepSeek 和查了一些关于模板库的书才搞出来的。Numbers 类只是闲心想写的,然后就这样了。还有 InputStream 和 OutputStream,只是快读快写 (小数和浮点数是真的不想写了)。
2. 快读快写
2.1 快读
没什么好讲的,除了判断符号还有每次左移。
2.2 快写
也没什么好讲的,是负数就输出 -,然后反转符号。
2.3 仿 C 函数
这个需要讲一下:看到 281~289 行,这是输入的仿函数的重载运算符。可以看看之前我的文章:面向对象编程。
由于 () 运算符是可以更改运算符的承受数的,所以我们使用类型安全的变长参数模板。由于是递归实现,所以要有一个终止函数。
但是,这个函数是一个即可以接受变量,又可以接受字面量的函数。在这里的字面量,称为右值。右值是没有命名的值,但是我们需要引用右值,这就要涉及到完美转发。
完美转发,就是将一些右值引用(它们是左值)转发成右值引用。可以看一下 PerfectRefrence 函数。它实际上是根据 std::move 函数的实现来写的。但它必定会是一个模板。
3. 异常
这个标准库里,异常都是根据 Exception 直接或间接来继承的。
为什么 Raise 函数也能够接受 EmptyError 之类的异常呢?因为这些类是向下转型,一直转型到积累 Exception 这个类,由于没有截断发生,所以可行。
4. 高精度
开袋即食。不需要我多讲了吧…… 除法的实现其实很简陋:如果除数比被除数小,那么向左移 位,直到左移 位会比被除数大。然后被除数减除数,除数右移 位。
5. 容器
5.1 标准库里的容器
在 STL 中,容器有以下几种:
- 有序容器
- 无序容器
- 关联容器
- 非联容器
这些容器,应该都是由底层的基类:Container 继承而来的。
5.2 容器的类型
由于 C++ 标准说明了,标准库中的容器必须是同构(一个容器中不能有不同类型的元素,非同构的我也实现不来)。所以,我实现了同构的容器。
5.3 类 / 模板
问:vector 是什么?
答:vector 是一个模板!
问:什么才是类?
答:vector<int> 等才算是类。
在标准库中,vector 被定义为一个模板,它的定义头类似:
template<typename T>注意到,T 表示的是它的元素类型。(Python 中的列表是可以多种类型一起存储的,这涉及到类型擦除等非常超纲の东西)C++11 标准要求这些标准库的容器应当是同构的。
5.4 容器适配器 / 底层适配器
在标准库中,有几种容器是根据其他容器实现来的。比如说 queue 的实现头类似这样:
template<typename T,typename Container=deque<T>>第一个依旧是元素的类型,但是第二个模板参数是这种容器的底层适配器。就像上面代码中的 Adapter 模板参数一样。
一般的,这些容器一般会提供一些必须方法,而不会暴露底层容器的接口。而且底层容器一般不需要自己指定!
6. 附录
更新预告:比较器 & 更新分配(vector)。