简体   繁体   English

如何对CArray的指针进行排序?

[英]How to sort a CArray of pointers?

As you might have already guessed I don't want to sort the pointer addresses but the objects/data. 正如您可能已经猜到的,我不想对指针地址进行排序,而是对对象/数据进行排序。

At the moment I have an array like this: 目前,我有一个这样的数组:

CArray <ReadObject *> readCollecion; 

and I sort it like that: 我这样排序:

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

Works perfectly with the keySortFunction. 与keySortFunction完美配合。

The problem is I need pointers to my objects because I need to modify the objects while they are already in the array. 问题是我需要指向对象的指针,因为我需要在对象已经存在于数组中时修改它们。 I guess I need and Array like this: 我想我需要这样的数组:

CArray <ReadObject *> readCollecion; 

Now I can change the objects afterwards but my sort seems to be unable to deal with this. 现在,我可以在以后更改对象,但是我的排序似乎无法处理。

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