Results 1 to 6 of 6

Thread: QList/QStringList comparison question

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QList/QStringList comparison question

    Is there a built in Qt function that compares two QLists or two QStringLists?
    I have two QStringLists, list1 and list2. I want to check if every QString in list1 is in list2 and if every QString in list2 is in list1. Order does not matter. Is there a built in Qt function or a built in C++ function to do this. Right now I am using for loops to do this, and I would rather not do this.

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList/QStringList comparison question

    Is there a built in Qt function that compares two QLists or two QStringLists?
    QtAssistant says:
    Qt Code:
    1. bool QList::operator== ( const QList<T> & other ) const
    To copy to clipboard, switch view to plain text mode 

    Returns true if other is equal to this list; otherwise returns false.

    Two lists are considered equal if they contain the same values in the same order.

    This function requires the value type to have an implementation of operator==().
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList/QStringList comparison question

    To complement what Artur already said, if you don't care about the order, you need to sort the two lists prior to comparing them.

    Qt Code:
    1. QStringList list1, list2;
    2. qSort(list1);
    3. qSort(list2);
    4. if(list1==list2){
    5. // ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: QList/QStringList comparison question

    I think you can do something like this:

    Qt Code:
    1. int check_lists(QStringList & list1, QStringList & list2) {
    2. int len_1 = list1.count();
    3. int len_2 = list2.count();
    4. int index;
    5. if (len_1 <= len_2) {
    6. for (index = 0;x < len_1; ++x) {
    7. if (!list2.contains(list1[index]))
    8. return 0;
    9. else {
    10. for (index = 0; x < len_2; ++x) {
    11. if (!list1.contains(list2[index]));
    12. return 0;
    13. return 1;
    14. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList/QStringList comparison question

    No, you can't.

    Try to compare the following lists your way:
    1. a, a, b
    2. a, b, b

    The lists are different, yet your comparison will say they are the same.
    Even if you consider the two above lists to be equal, the following two are certainly not and your function will still say they are:
    1. a, b, c
    2. a, b, c, d
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: QList/QStringList comparison question

    Ops...
    I thought it was:
    "I want to see if all elements of list1 are in list2 with "order-insensitive" mode."
    and not:
    "I want to see if all elements of list1 are in list2 and viceversa with "order-insesitive" mode."
    sry ^^

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.