Difference between copy_backward and reverse_copy?

I am reading a C ++ primer and I see these two functions that seem to have the same functionality. Can someone help and tell me what is the difference between the two? Thanks.

+5
source share
2 answers

reverse_copy actually puts items in reverse order.

 1 2 3 4 5 - > 5 4 3 2 1 

copy_backward simply copies the items back, but preserves their relative order.

 1 2 3 4 5 

5 is first copied, but placed in last place. So your result is still:

 1 2 3 4 5 

http://en.cppreference.com/w/cpp/algorithm/copy_backward

Copy elements from the range defined by [first, last] to another range ending in d_last. Elements are copied in the reverse order (the last element is copied first) , but their relative order is preserved .

http://en.cppreference.com/w/cpp/algorithm/reverse_copy

Copies elements from the range [first, last] to another range starting with d_first so that the elements in the new range are in reverse order.

+6
source

std::copy_backwards :

Copy elements from the range defined by [first, last] to another range ending in d_last. Elements are copied in the reverse order (the last element is copied first), but their relative order is preserved.

std::reverse_copy

Copies elements from the range [first, last] to another range starting with d_first so that the elements in the new range are in reverse order.

Thus, the difference is that std::copy_backwards starts copying at the end and works in the opposite direction, preserving the original positioning, while std::reverse_copy starts copying at the beginning forward, but puts them in the reverse order.

+1
source

Source: https://habr.com/ru/post/1237273/


All Articles