比较QList qt5.3中的项目

时间:2020-11-28 22:51:20

I am trying to compare the items in a QList. Here is the old way to do it using QPtrCollection but this cannot be used in versions after qt3 (as far as I'm aware).

我正在试着比较一个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());}
};

I can't figure out what a good way of doing this in Qt5.3 might be?

我不知道Qt5.3中这样做的一个好方法是什么?

1 个解决方案

#1


2  

You can use the std::equal algorithm on QList objects, as in:

您可以在QList对象上使用std::等算法,如:

#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;
                             } );
}

#1


2  

You can use the std::equal algorithm on QList objects, as in:

您可以在QList对象上使用std::等算法,如:

#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;
                             } );
}