Issues with String reverse_copy Solution


In this article we will discuss issues faced using reverse_copy(), while trying to simply reverse a string. The following piece of code does not reverse the string as expected. i.e. ‘A’ is a string “abc” and ‘B’ is the result of reversed string that needs to be “cba”. But, this does not happen as expected in the below code.

Issues with String reverse_copy

Cause of the problem:

The problem here is that, the code std::reverse_copy by default expects that you pre allocate space in the destination string, where you intend to copy the reversed string. Std:reverse_copy does not allocate space on its own.


There are two options to overcome this:

For string reversal to happen you need to allocate space for the destination string as below:

Issues with String reverse_copy

Another option would be to use std: back_inserter, this will push characters at the back of the target string. Hence, it also works on an empty string:

Issues with String reverse_copy

Solution Worked 0 UpvotesUpvote


Solution Didn't Worked 0 DownvotesDownvote



Play 2048 Game Online

Search Tags