gpt4 book ai didi

c++ - 比较QList qt5.3中的项目

转载 作者:行者123 更新时间:2023-11-30 04:03:38 32 4
gpt4 key购买 nike

我正在尝试比较 QList 中的项目。这是使用 QPtrCollection 执行此操作的旧方法,但这不能在 qt3 之后的版本中使用(据我所知)。

class gnyComponentList:public QList<gnyComponent>
{
protected:
virtual int compareItems ( QPtrCollection::Item item1, QPtrCollection::Item item2 )
{ return (((gnyComponent *)item1)->getID()).compare(((gnyComponent *)item2)->getID());}
};

我想不出在 Qt5.3 中这样做的好方法是什么?

最佳答案

您可以使用 std::equal QList 对象上的算法,如:

#include <QList>
#include <QString>

#include <algorithm> // for std::equal

struct Person
{
QString firstName;
QString lastName;
};

int main()
{
QList<Person> personsA, personsB;
// Populate personsA and personsB
bool equal = std::equal( personsA.begin(), personsA.end(),
personsB.begin(),
[]( const Person &a, const Person & b ) {
return a.firstName == b.firstName;
} );
}

关于c++ - 比较QList qt5.3中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24306497/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com