2011-03-25 102 views

回答

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); 

     } 
    } 
} 
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;