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 开发者_StackOverflowhave 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.
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.
If I'm understanding your question correctly, all you need to do is change the parameter type of keySortFunction
from const ReadObject&
to const ReadObject*
and make the appropriate changes to the function to use ->
instead of .
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);
});
精彩评论