代码之家  ›  专栏  ›  技术社区  ›  TalkingCode

如何对指针进行排序?

  •  1
  • TalkingCode  · 技术社区  · 16 年前

    正如您可能已经猜到的,我不想对指针地址进行排序,而是对对象/数据进行排序。

    目前我有一个这样的阵列:

    CArray <ReadObject *> readCollecion; 
    

    我是这样分类的:

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

    与keySortFunction完美配合。

    问题是我需要指向我的对象的指针,因为我需要在对象已经在数组中时修改它们。我想我需要这样的排列:

    卡雷<ReadObject*>readCollecion;
    

    现在我可以在事后更改对象,但我的同类似乎无法处理这个问题。

    2 回复  |  直到 11 年前
        1
  •  0
  •   Mark B    16 年前

    如果我正确理解了你的问题,你需要做的就是更改 keySortFunction 从…起 const ReadObject& const ReadObject* 并对要使用的函数进行适当的更改 -> 而不是 .

        2
  •  0
  •   Andrey    11 年前
    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);
    });