www.pudn.com > SendMsgOCX.rar > ArrayTempl.h
#ifndef __ARRAYTEMPL_H__ #define __ARRAYTEMPL_H__ #includetemplate < class TYPE, class ARG_TYPE > class CMyArray : public CArray< TYPE, ARG_TYPE > { public: //find element == "toFind" and return the index //return -1 if not found int Find( ARG_TYPE toFind, int nStart = 0 ) const; //add the "toAdd" in order //return the index to element just added int AddInOrder( ARG_TYPE toAdd ); //delete all items what is in "toCmp" //for example: //this array has value: 1, 2, 7, 8, 4, 4 //to toCmp has value: 2, 4, 8 //after this operation, values in this array may be: 1, 7 ( 2, 8, 4 ) is deleted //return number of items deleted int RemoveDupItem( const CMyArray &toCmp ); //delete all items with value same to (toDel) //return the number of items deleted int RemoveItem( ARG_TYPE toDel ); const CMyArray &operator = ( const CMyArray & toCopy ); protected: //compare the ele1 and ele2 inline int Compare( ARG_TYPE ele1, ARG_TYPE ele2 ) const; }; //NOTE: template < class TYPE, class ARG_TYPE > int CMyArray< TYPE, ARG_TYPE > :: Find( ARG_TYPE toFind, int nStart ) const { int nNumber = GetSize(); for( int i = nStart; i < nNumber; i ++ ) if( 0 == Compare(GetAt(i), toFind) ) return i; return -1; } template < class TYPE, class ARG_TYPE > int CMyArray< TYPE, ARG_TYPE > :: AddInOrder( ARG_TYPE toAdd ) { //find position to insert int nSize = GetSize(); int i = 0; while( i < nSize ) if( Compare( GetAt( i++ ), toAdd ) >= 0 ) break; //the first element >= toAdd //insert InsertAt( i, toAdd ); return i; } template < class TYPE, class ARG_TYPE > const CMyArray & CMyArray :: operator = ( const CMyArray & toCopy ) { RemoveAll(); Copy( toCopy ); return *this; } template < class TYPE, class ARG_TYPE > int CMyArray :: RemoveDupItem( const CMyArray &toCmp ) { int nSize = GetSize(); int nDeleted = 0; int i = 0; while( i < nSize ) { if( toCmp.Find( GetAt(i) ) >= 0 ) { RemoveAt(i); nSize--; //i not changed nDeleted++; } else i++; } return nDeleted; } template < class TYPE, class ARG_TYPE > int CMyArray :: RemoveItem( ARG_TYPE toDel ) { int nSize = GetSize(); int nDeleted = 0; int i = 0; while( i < nSize ) { if( Compare( GetAt(i), toDel ) == 0 ) { RemoveAt(i); nDeleted++; nSize--; } else i ++; } return nDeleted; } template < class TYPE, class ARG_TYPE > int CMyArray :: Compare( ARG_TYPE ele1, ARG_TYPE ele2 ) const { return ele1 - ele2; //default } #endif //__ARRAYTEMPL_H__