copy_backward 和 reverse_copy 的区别?
Difference between copy_backward and reverse_copy?
我正在阅读 C++ primer,看到这两个函数似乎具有相同的功能。谁能帮忙告诉我这两者有什么区别?谢谢
reverse_copy
实际上是将元素倒序排列。
1 2 3 4 5 - > 5 4 3 2 1
copy_backward
简单地向后复制元素,但保留它们的相对顺序。
1 2 3 4 5
5先复制,但放在最后一个位置。所以你的输出仍然是:
1 2 3 4 5
http://en.cppreference.com/w/cpp/algorithm/copy_backward
Copies the elements from the range, defined by [first, last), to another range ending at d_last. The elements are copied in reverse order (the last element is copied first), but their relative order is preserved.
http://en.cppreference.com/w/cpp/algorithm/reverse_copy
Copies the elements from the range [first, last) to another range beginning at d_first in such a way that the elements in the new range are in reverse order.
Copies the elements from the range, defined by [first, last), to another range ending at d_last. The elements are copied in reverse order (the last element is copied first), but their relative order is preserved.
Copies the elements from the range [first, last) to another range beginning at d_first in such a way that the elements in the new range are in reverse order.
所以不同的是std::copy_backwards
从最后开始复制,然后向后复制,保持原来的位置,而std::reverse_copy
从头开始复制,但顺序相反。
我正在阅读 C++ primer,看到这两个函数似乎具有相同的功能。谁能帮忙告诉我这两者有什么区别?谢谢
reverse_copy
实际上是将元素倒序排列。
1 2 3 4 5 - > 5 4 3 2 1
copy_backward
简单地向后复制元素,但保留它们的相对顺序。
1 2 3 4 5
5先复制,但放在最后一个位置。所以你的输出仍然是:
1 2 3 4 5
http://en.cppreference.com/w/cpp/algorithm/copy_backward
Copies the elements from the range, defined by [first, last), to another range ending at d_last. The elements are copied in reverse order (the last element is copied first), but their relative order is preserved.
http://en.cppreference.com/w/cpp/algorithm/reverse_copy
Copies the elements from the range [first, last) to another range beginning at d_first in such a way that the elements in the new range are in reverse order.
Copies the elements from the range, defined by [first, last), to another range ending at d_last. The elements are copied in reverse order (the last element is copied first), but their relative order is preserved.
Copies the elements from the range [first, last) to another range beginning at d_first in such a way that the elements in the new range are in reverse order.
所以不同的是std::copy_backwards
从最后开始复制,然后向后复制,保持原来的位置,而std::reverse_copy
从头开始复制,但顺序相反。