std :: copy句柄是否重叠范围? [英] Does std::copy handle overlapping ranges?
问题描述
将数据从一个范围复制到另一个范围时,如果源和目标范围之间存在部分重叠,则必须小心。如果目标范围的开始与源范围的尾部重叠,则平滑顺序副本将会使数据乱码。除了 memcpy
之外,C运行时库还具有 memmove
以处理此类重叠问题。
When copying data from one range to another, you have to be careful if there's partial overlap between the source and destination ranges. If the beginning of the destination range overlaps the tail of the source range, a plain sequential copy will garble the data. The C run-time library has memmove
in addition to memcpy
to handle such overlap problems.
我假设 std :: copy
的工作原理像 memcpy
不考虑源和目的地区域之间的重叠。如果您尝试在 std :: vector
中使用 std :: copy
将对象向下损坏数据。是否有类似 memmove
的STL算法来处理这种情况?
I assume std::copy
works like memcpy
, in that it doesn't pay any regard to overlap between the source and destination regions. If you try to shift objects "down" in a std::vector
with std::copy
, you'll corrupt the data. Is there an STL algorithm analogue of memmove
to handle situations like this? Or should I roll my own with reverse iterators?
推荐答案
它不处理重叠范围iff the 开始
It doesn't handle overlapping ranges iff the beginning of the output range overlaps with the input range.
幸运的是,您可以使用 std :: copy_backward
具有输入范围的输出范围的 end )。
Fortunately, you can use std::copy_backward
instead (which requires that you don't overlap the end of the output range with the input range).
这篇关于std :: copy句柄是否重叠范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!