?? arraytempl.h
字號:
#ifndef __ARRAYTEMPL_H__
#define __ARRAYTEMPL_H__
#include <afxtempl.h>
template < 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<TYPE,ARG_TYPE> &toCmp );
//delete all items with value same to (toDel)
//return the number of items deleted
int RemoveItem( ARG_TYPE toDel );
const CMyArray<TYPE, ARG_TYPE> &operator = ( const CMyArray<TYPE, ARG_TYPE>& 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<TYPE, ARG_TYPE>& CMyArray<TYPE, ARG_TYPE>::
operator = ( const CMyArray<TYPE, ARG_TYPE>& toCopy )
{
RemoveAll();
Copy( toCopy );
return *this;
}
template < class TYPE, class ARG_TYPE >
int CMyArray<TYPE, ARG_TYPE>::
RemoveDupItem( const CMyArray<TYPE,ARG_TYPE> &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<TYPE, ARG_TYPE>::
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<TYPE, ARG_TYPE>::
Compare( ARG_TYPE ele1, ARG_TYPE ele2 ) const
{
return ele1 - ele2; //default
}
#endif //__ARRAYTEMPL_H__
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -