Results 1 to 10 of 10

Thread: auto-sorted qtl container

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default auto-sorted qtl container

    I am using a std::set<int> to keep a list of sorted ints. The set is automatically sorted, so i can add a value at any time, and the set will be reordered.

    The QTL equivalent (?) however, is QHash-based, which i would seem unnecessary here. The items are also not sorted.

    So what's the QTL equivalence of the STL set<int> ?

    Qt Code:
    1. std::set<int> sss;
    2. sss.insert(0x100);
    3. sss.insert(0x10);
    4. sss.insert(0);
    5. sss.insert(0x1000);
    6. //sss -> 0,0x10,0x100,0x1000
    7.  
    8.  
    9. QSet<int> aaa;
    10. aaa.insert(0x100);
    11. aaa.insert(0x10);
    12. aaa.insert(0);
    13. aaa.insert(0x1000);
    14. //aaa -> 0x100,0x10,0,0x1000
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: auto-sorted qtl container

    Hmmm.... QSet?
    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.


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

    Default Re: auto-sorted qtl container

    Quote Originally Posted by wysota View Post
    Hmmm.... QSet?
    That is not ordered either
    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.

  4. #4
    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: auto-sorted qtl container

    A set is not ordered. An ordered set is a list.
    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.


  5. #5
    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: auto-sorted qtl container

    A std::set is ordered and contains no duplicates (unlike a list). QSet, on the other hand, does not preserve order: it is closer to the mathematical notion of a set.

    Using QMap keys only would be the closest match if uniqueness and order is important.
    Qt Code:
    1. QMap<int,int> sss;
    2. sss.insert(0x100, 0); // value is irrelevant
    3. sss.insert(0x10, 0);
    4. sss.insert(0, 0);
    5. sss.insert(0x1000, 0);
    6. qDebug() << sss.keys();
    7. // (0, 16, 256, 4096)
    To copy to clipboard, switch view to plain text mode 

    If uniqueness is not a requirement then a simple QList that you sort before access is a good match.

  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: auto-sorted qtl container

    You can always use this simple scheme:

    Qt Code:
    1. QList<int> list;
    2.  
    3. int valueToInsert = 42;
    4. QList::iterator iter = qLowerBound(list.begin(), list.end(), valueToInsert);
    5. if(iter == list.end() || *iter != valueToInsert) list.insert(iter, valueToInsert);
    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.


  7. #7
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: auto-sorted qtl container

    Kay, so taht's not possible. Using QMap with a dummy .data field seems a little too messy to me, using QList<> as suggested by wysota is ofcourse possible.
    Thanks all, guess i'll go with that.

    Lil follow-up problem:
    I can only get const References to the items contained in the list. Why?

    Qt Code:
    1. MyClass& LeFunc(...)
    2. {
    3. QList<MyClass> list;
    4.  
    5. ...
    6.  
    7. Q_FOREACH(MyClass& i, list) //error C2440: 'initializing' : cannot convert from 'const MyClass' to 'MyClass &'
    8. {
    9. if(i.thisisit())
    10. return i;
    11. }
    12.  
    13. ...
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: auto-sorted qtl container

    Because foreach makes a copy of the container. It's for immutable loops only.
    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.


  9. #9
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: auto-sorted qtl container

    Makes snese.
    Although this doesnt work either:



    Qt Code:
    1. for(QList<MyClass>::iterator it=list.begin(); it != list.end(); ++it) //error C2440: 'initializing' : cannot convert from 'QList<T>::const_iterator' to 'QList<T>::iterator'
    2. {
    3. if(it->thisisit())
    4. return *it;
    5. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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: auto-sorted qtl container

    Apparently your "list" is const. Why do you insist on using a non-const iterator here?
    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. reload qtableview after sorted
    By poporacer in forum Newbie
    Replies: 0
    Last Post: 18th April 2011, 21:07
  2. List View with sections for alphabetialy sorted list
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2010, 11:50
  3. sorted table
    By reshma in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2009, 03:47
  4. Quick ? about qSort(Container & container)
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2007, 11:20
  5. Currently sorted column in QTableView
    By borges in forum Newbie
    Replies: 1
    Last Post: 21st September 2007, 16:52

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.