0
我想要一些模板类,如排序等用于排序CTypedPtrList。如何在C++或MFc中排序CTypedPtrList?
我想要一些模板类,如排序等用于排序CTypedPtrList。如何在C++或MFc中排序CTypedPtrList?
该样品将整理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);
}
}
}
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;
您还没有告诉我们您想要排序的指针或指向的数据。 – 2011-03-25 18:06:27
'c'标记已移除 – pmg 2011-03-30 08:20:18