Results 1 to 4 of 4

Thread: Sorting a qLinkedList

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sorting a qLinkedList

    Is it possible to sort a qLinkedList structure? The qLinkedList doesn't seem to have random access iterators, so the compiler complains when I try and pass the linked list to the sorting function. Simple code is shown below; if the qLinkedList is changed to qList then everything is fine, but my application needs iterators to remain valid after sorting, so qList/qVector aren't suitable.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QLinkedList>
    3. #include <QtAlgorithms>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QLinkedList<int> list;
    10. list << 5 << 2 << 7 << 12;
    11. qSort (list.begin, list.end());
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

    EDIT: It looks like the problem may be a relatively recent one. In Qt 4.1 qSort only required bidirectional iterators, which qLinkedList would support. In Qt 4.6 the requirement has been bumped up to random access iterators.
    Last edited by pwaldron; 16th January 2010 at 13:47.

  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: Sorting a qLinkedList

    Did you try using std::sort()?
    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
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting a qLinkedList

    Same problem. std::sort requires a random access iterator, not a bidirectional one.

  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: Sorting a qLinkedList

    Both std::sort and qSort implement QuickSort and this one indeed needs a random access iterator. You can try doing bubble sort or insert sort, this should work on iterators with more restrictions.
    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. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. QList or QLinkedList
    By eleanor in forum Newbie
    Replies: 6
    Last Post: 9th November 2007, 09:40
  3. QLinkedList iterator
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 18th October 2007, 16:15
  4. QLinkedList and iterators
    By eu.x in forum Newbie
    Replies: 1
    Last Post: 19th April 2007, 19:38
  5. Table Sorting
    By ToddAtWSU in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2007, 14:49

Tags for this Thread

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.