簡體   English   中英

如何對CArray的指針進行排序?

[英]How to sort a CArray of pointers?

正如您可能已經猜到的,我不想對指針地址進行排序,而是對對象/數據進行排序。

目前,我有一個這樣的數組:

CArray <ReadObject *> readCollecion; 

我這樣排序:

std::sort(readCollecion.GetData(), readCollecion.GetData()+readCollecion.GetSize(), keySortFunction);

與keySortFunction完美配合。

問題是我需要指向對象的指針,因為我需要在對象已經存在於數組中時修改它們。 我想我需要這樣的數組:

CArray <ReadObject *> readCollecion; 

現在,我可以在以后更改對象,但是我的排序似乎無法處理。

bool keySortFunction(const ReadObject& o1, const ReadObject& o2)
{
    return ...;
}

CArray <ReadObject> readCollecion; 
std::sort(readCollecion.GetData(), readCollecion.GetData()+readCollecion.GetSize(), eySortFunction);

...

CArray <ReadObject*> readCollecion2;
std::sort(readCollecion2.GetData(), readCollecion2.GetData()+readCollecion2.GetSize(), [](ReadObject* o1, ReadObject* o2)
{
    return keySortFunction(*o1, *o2);
});

如果我正確地理解了您的問題,那么您要做的就是將keySortFunction的參數類型從const ReadObject&更改為const ReadObject* ,並對函數進行適當的更改以使用->而不是.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM