Results 1 to 5 of 5

Thread: Basic <algorithm> (reverse_copy) doesn't work with QString

  1. #1
    Join Date
    Jul 2016
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Basic <algorithm> (reverse_copy) doesn't work with QString

    Following code returns empty string, why?

    Qt Code:
    1. QString reverseQString( const QString & str )
    2. {
    3. QString rev;
    4. reverse_copy( str.begin(), str.end(), rev.begin() );
    5. return rev;
    6. }
    To copy to clipboard, switch view to plain text mode 


    Added after 27 minutes:


    Is the problem that reverse_copy in fact needs functionality of std::back_inserter, not just an iterator?
    Last edited by AnthonyL; 27th August 2016 at 16:43.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Basic <algorithm> (reverse_copy) doesn't work with QString

    Hello,

    the local object rev is an empty string. std::reverse_copy expects that the destination container (in your case rev) already has allocated memory to store the reverse copy. To achieve this, add
    Qt Code:
    1. rev.resize(str.size());
    To copy to clipboard, switch view to plain text mode 
    before calling reverse_copy.

    See also http://www.cplusplus.com/reference/a.../reverse_copy/
    Best regards
    ars

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Basic <algorithm> (reverse_copy) doesn't work with QString

    My guess would be that QString::begin() returns QString::end() when called on a null QString and one cannot assign to end().
    I.e. QString::begin() of a null QString might not qualify as an OutputIterator.

    You could try with a std::back_inserter.

    Cheers,
    _

  4. #4
    Join Date
    Jul 2016
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Basic <algorithm> (reverse_copy) doesn't work with QString

    Quote Originally Posted by ars View Post
    Hello,

    the local object rev is an empty string. std::reverse_copy expects that the destination container (in your case rev) already has allocated memory to store the reverse copy. To achieve this, add
    Qt Code:
    1. rev.resize(str.size());
    To copy to clipboard, switch view to plain text mode 
    before calling reverse_copy.

    See also http://www.cplusplus.com/reference/a.../reverse_copy/
    Best regards
    ars
    It then works, thanks

  5. #5
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Basic <algorithm> (reverse_copy) doesn't work with QString

    Using std::back_inserter makes use of a sequence of QString:ush_back() calls, which result in multiple memory reallocations (depending on the memory allocation strategy implemented in QString:ush_back()). In the scenario posted by OP we already know the size of the result, so I expect better performance using a single memory reallocation than a sequence of reallocations. Of course this will not give measurable differences for small containers/strings.

    Best regards
    ars

Similar Threads

  1. Replies: 0
    Last Post: 19th April 2015, 19:32
  2. Replies: 0
    Last Post: 29th April 2011, 10:02
  3. Replies: 6
    Last Post: 26th January 2011, 22:00
  4. Basic QMainWindow doesn't show menubar
    By Jeffb in forum Newbie
    Replies: 3
    Last Post: 14th April 2010, 14:52
  5. QString::fromStdWString() doesn't work ...
    By jsmax in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2006, 18:02

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.