Results 1 to 20 of 22

Thread: Sorting a QVector

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    9
    Thanked 29 Times in 29 Posts

    Default Re: Sorting a QVector

    Quote Originally Posted by wysota View Post
    The trivial implementation of swap is something like:
    I wasn't thinking of the trivial implementation. Because of exactly the drawbacks you describe.

    Quote Originally Posted by wysota View Post
    qSwap() tries to be smarter, for instance it can detect if operator=() needs to be used or if it can use memcpy() or similar, thus it is faster for movable types.
    operator=() should never be used for a swap operation. Why use deep copies if, after the function call, you still end up with exactly the same dynamic data? A shallow copy only moves the pointers around (and the primitive types). For the swap function this should be enough. And I'd be surprised if the standard C++ implementation did it differently.

    Of course, this is only true for reasonable implementations of operator=(). But one can't assume that operator=() is going to be used in swap(). All that is required after the swap is that X behaves as Y did and Y behaves as X did.
    Last edited by Michiel; 11th August 2007 at 00:28.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 17:46
  2. QVector problem
    By kingslee in forum Qt Programming
    Replies: 5
    Last Post: 19th October 2006, 11:42
  3. Column Sorting
    By sumsin in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 08:48
  4. QT4: Sorting in QTreeWidget (subclass)
    By Michiel in forum Qt Programming
    Replies: 21
    Last Post: 29th March 2006, 19:08
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 21:16

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
  •  
Qt is a trademark of The Qt Company.