template<classTYPE>classCTArray{//动态数组类private:UINTnSize;//actualsizeUINTnGrow;//growfactorprotected:UINTnItems;//numberofelements(asitappearstotheuser)TYPE*pData;//pointertoarrayofdatapublic://blankconstructorCTArray(){Init();}//copyconstructorCTArray(CTArray&Src){Init();Copy(Src);}//typedcopyconstructorCTArray(constTYPE*pSrc,UINTnCount=1){Init();SetLength(nCount);for(UINTu=0;u<nItems;u++)pData[u]=pSrc[u];}//typedinitializingconstructorCTArray(UINTnCount,TYPESrc){Init();SetLength(nCount);for(UINTu=0;u<nItems;u++)pData[u]=Src;}//operatorsconstTYPE&operator[](UINTnIndex)const{returnpData[nIndex];}TYPE&operator[](UINTnIndex){returnpData[nIndex];}CTArray&operator=(CTArray&Src){Copy(Src);return*this;}//initialisevoidInit(){pData=NULL;nSize=nItems=0;nGrow=8;}//releasedatavirtualvoidClear(void){if(pData!=NULL)delete[]pData;Init();}//copyfromothervoidCopy(CTArray&Src){Clear();SetLength(Src.Length());for(UINTu=0;u<nItems;u++)pData[u]=Src.pData[u];}//growfactorget/setUINTGrowFactor(void)const{returnnGrow;}voidSetGrowFactor(UINTnNewGrow){nGrow=nNewGrow;if(nGrow==0)nGrow=1;}//length(items)getUINTLength(void){returnnItems;}//setlengthregroworshrinkvirtualboolSetLength(UINTnLength,boolbForce=false){if(nLength==0){Clear();returntrue;}//allocnewstorageTYPE*pNewData=NULL;UINTnNewSize=((nLength/nGrow)+1)*nGrow;//新数组大小//growonlyifeithertheamountweneedisgreaterthanwhatwehave//alreadyoriftheamountis<=1/2,whatever'ssmallerif(nNewSize>nSize||nNewSize<=nSize/2||bForce){//创建新数组if((pNewData=newTYPE[nNewSize])==NULL)returnfalse;//nowcopytheoldelementsintothenewarray,uptotheold//numberofitemsortotheuser-setnewlength,whichever's//smallerfor(UINTu=0;u<nItems&&u<nLength;u++)pNewData[u]=pData[u];//updateallthecurrentinfoif(pData!=NULL)delete[]pData;pData=pNewData;nSize=nNewSize;}nItems=nLength;returntrue;}//setw/boundscheckbutnogrowvirtualboolSet(UINTnIndex,TYPESrc)const{if(nIndex>=nItems||pData==NULL)returnfalse;pData[nIndex]=Src;returntrue;}//getw/boundscheckbutnogrowvirtualboolGet(TYPE&Dst,UINTnIndex)const{if(nIndex>=nItems||pData==NULL)returnfalse;Dst=pData[nIndex];returntrue;}//getallelementstoatypedpointer;donotforgettodelete//suchpointerafternolongerneededUINTGetAll(TYPE*&pDst){pDst=newTYPE[nItems];for(UINTu=0;u<nItems;u++)pDst[u]=pData[u];returnnItems;}//getallelementstoanunknownsizepointerofspecifiedsize;the//pointermustbeinitializedbythecallerUINTGetAll(void*pDst,intnSize){for(UINTu=0;u<nItems;u++)memcpy((void*)((BYTE*)pDst+u*nSize),(void*)(&pData[u]),nSize);returnnItems;}//removeelementatgivenpositionvirtualboolRemove(UINTnIndex){if(nItems==0||pData==NULL)returnfalse;//startingwiththeelementweareremoving,workup//copyingeachnextvaluedowntothecurrentspotfor(UINTu=nIndex;u<nItems-1;u++)pData[u]=pData[u+1];//thiswilleithersimplychangethenItemsvalueorreallocand//freesomememorySetLength(nItems-1);returntrue;}//insertelementatgivenpositionvirtualvoidInsert(TYPESrc,UINTnIndex){//first,makeroomSetLength(nItems+1);//startingwiththelastelementworkbackuntilwegettotheone//weareinsertingatandcopyforwardfor(UINTu=nItems-1;u>nIndex;u--)pData[u]=pData[u-1];//finallyinsertnewvaluepData[nIndex]=Src;}//appendelementtotheendofarrayvirtualintAppend(TYPESrc){//first,makeroomSetLength(nItems+1);//insertnewvaluepData[nItems-1]=Src;returnnItems;}//blankappendvirtualintAppend(){//justmakeroomSetLength(nItems+1);returnnItems;}//finderwithmemcompareandoptionalstartintFind(TYPESrc,UINTnStart=0){for(UINTu=nStart;u<nItems;u++){if(memcmp(&pData[u],&Src,sizeof(TYPE))==0)return(int)u;}return-1;}//swapvoidSwap(UINTi,UINTj){if(i>=nItems||j>=nItems||i==j)return;TYPETmp=pData[i];pData[i]=pData[j];pData[j]=Tmp;}//sortwrapperwithcallbackandmethodvoidSort(int(__cdecl*compare)(constvoid*p1,constvoid*p2),intnMethod=0){switch(nMethod){case1:{//sortthearraywithfixedstartingitems,bycomparingneighborsfor(UINTi=0;i<nItems-1;i++){//skipaneighborthatisinorder(asdeterminedbyanon-zero//returnfromthecomparefunction)if(compare((constvoid*)&pData[i],(constvoid*)&pData[i+1]))continue;//searchforanitemmatchingthelaststartingitemUINTj=i+1;while(!compare((constvoid*)&pData[i],(constvoid*)&pData[j])&&j<nItems-1)j++;//swapthematchingitemtoberightbelowthestartingitemif(j<=nItems-1)Swap(i+1,j);}break;}default:qsort(pData,nItems,sizeof(TYPE),compare);break;}}//destructor~CTArray(){Clear();}};typedefCTArray<DWORD>DWORDARRAY;