我想要一些模板类如sort等用于对CTypedPtrList进行排序。
答案 0 :(得分:3)
此示例将对CTypedPtrArray进行排序:
typedef CTypedPtrArray<CPtrArray , CLog *> CLogData;
CLogData tLogData;
CLog *t1Log , * t2Log;
bool bChanged = true;
if (tLogData.IsEmpty())
return;
long int i, j;
for (i = 0 ; i < m_nCount - 1 ; i++)
{
for( j = i + 1; j < m_nCount ; j++ )
{
t1Log = tLogData.GetAt( i);
t2Log = tLogData.GetAt( j ) ;
if (strcmp(t1Log->GetThreadName() , t2Log->GetThreadName()) > 0)
{
tLogData.SetAt( i , t2Log );
tLogData.SetAt( j , t1Log );
}
}
}
答案 1 :(得分:0)
karthik的解决方案不适用于CTypedPtrList,因为CTypePtrList不支持.GetAt()和.SetAt();这是一个使用指针列表而不是数组的解决方案。
typedef CTypedPtrList<CPtrList, CMyObject*> ObjectList;
// sort object list using CMyObject's 'order' attribute
ObjectList* oldList = Objects;
Objects = new ObjectList();
for (POSITION pos1 = oldList->GetHeadPosition(); pos1 != NULL;)
{
CMyObject *obj1 = oldList->GetNext(pos1);
POSITION pos2 = Objects->GetHeadPosition();
bool inserted = false;
while (pos2 != NULL)
{
POSITION currentPos = pos2;
CMyObject *obj2 = Objects->GetNext(pos2);
if (obj1->GetOrder() < obj2->GetOrder())
{
Objects->InsertBefore(currentPos, obj1);
pos2 = NULL;
inserted = true;
}
}
if (!inserted) Objects->AddTail(obj1);
}
delete oldList;