今天把项目里的一个STL容器替换掉,并不是因为效率问题。
现在可以把这个东西分享一下:
template <class Element, INT32 N>
struct ElementArr{
struct ArrInside{
Element ele;
BOOLEAN isVaild;
void Clear(){
isVaild = FALSE;
}
};
ArrInside m_Arr[N];
INT32 m_CurSize;
INT32 GetMaxSize(){
return N;
}
INT32 AddElement(Element element){
if (m_CurSize == N){
return eNormal;
}
for (INT32 i = 0; i < N; ++i){
if (FALSE == m_Arr[i].isVaild){
m_Arr[i].ele = element;
m_Arr[i].isVaild = TRUE;
++m_CurSize;
break;
}
}
return eNormal;
}
BOOLEAN IsHvElement(Element element){
if (m_CurSize == 0){
return FALSE;
}
for (INT32 i = 0; i < N; ++i){
if (TRUE == m_Arr[i].isVaild && m_Arr[i].ele == element){
return TRUE;
}
}
return FALSE;
}
INT32 RemoveElement(Element element){
if (m_CurSize == 0){
return eNormal;
}
for (INT32 i = 0; i < N; ++i){
if (m_Arr[i].isVaild && m_Arr[i].ele == element){
m_Arr[i].isVaild = FALSE;
--m_CurSize;
break;
}
}
return eNormal;
}
INT32 GetCurSize(){
return m_CurSize;
}
VOID Clear(){
for (INT32 i = 0; i < N; ++i){
m_Arr[i].Clear();
}
m_CurSize = 0;
}
};
还是挺好用,但是挺丑