2011-03-25 7 views

답변

3

이 샘플 CTypedPtrArray를 정렬합니다 : CTypePtrList이 .GetAt()와 .SetAt()를 지원하지 않는

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 작동하지 않습니다; 여기에 배열 대신 포인터 목록을 사용하는 해결책이 있습니다.

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; 
관련 문제