Results 1 to 6 of 6

Thread: how to confirm the value of one QList in other QList quickly?

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default how to confirm the value of one QList in other QList quickly?

    Now,I have two group values(the type of it are int),and each group has many values,for example,100000.
    I put them in two QList seperately,now I want to know each value in QList A exists in QList B or not quickly?
    How to do?
    Could you tell me if there is other method,that is,QList not used?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to confirm the value of one QList in other QList quickly?

    You could just brute force search the lists using QList::contains().

    If the lists are sorted then you could do something like:
    Qt Code:
    1. QList<int>::const_iterator a = listA.constBegin();
    2. QList<int>::const_iterator b = listB.constBegin();
    3. while (a != listA.constEnd() && b != listB.constEnd()) {
    4. if (*a > *b)
    5. ++b;
    6. else {
    7. if (*a == *b)
    8. qDebug() << *a;
    9. ++a;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    If the lists contain no repeats then you could merge the lists, sort the compound list, and look for duplicates.


    You could use a QHash<int, int> or QMap<int, int> for list B (value is just 1 or the number of instances of the key) to speed up lookup.
    You could use an SQL database and query.
    Lots of things affect the possibilities. Do you know anything about the values? Are they in order and does order matter? Are there duplicate values? What level of duplication? What form is the data in before you put it in the lists? Are you interested only in existence or number of instances?

  3. #3
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to confirm the value of one QList in other QList quickly?

    the two group values are both in order.Could you tell me the more efficient method?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to confirm the value of one QList in other QList quickly?

    There is no perfect method... just one that works well enough.

    Try the code I posted and see if it is fast enough. On my machine it gets through two sorted 100000 entry lists of random numbers , building a third list, in 1 or 2 milliseconds (and I think it even gives the correct answer for some definition of correct).

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to confirm the value of one QList in other QList quickly?

    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    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: how to confirm the value of one QList in other QList quickly?

    Quote Originally Posted by ChrisW67 View Post
    Try the code I posted and see if it is fast enough.
    I don't think you can get faster than O(n+m) complexity which this algorithm has. At least not without knowing characteristics of the data and focusing only on SISD approach (you could make it faster with SIMD).
    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.


Similar Threads

  1. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  2. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  3. Replies: 4
    Last Post: 20th August 2010, 13:54
  4. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  5. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42

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.