Following code returns empty string, why?
Added after 27 minutes:
Is the problem that reverse_copy in fact needs functionality of std::back_inserter, not just an iterator?
Printable View
Following code returns empty string, why?
Added after 27 minutes:
Is the problem that reverse_copy in fact needs functionality of std::back_inserter, not just an iterator?
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
before calling reverse_copy.Code:
rev.resize(str.size());
See also http://www.cplusplus.com/reference/a.../reverse_copy/
Best regards
ars
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,
_
Using std::back_inserter makes use of a sequence of QString::push_back() calls, which result in multiple memory reallocations (depending on the memory allocation strategy implemented in QString::push_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