简体   繁体   English

std :: copy是否处理重叠范围?

[英]Does std::copy handle overlapping ranges?

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. 除了memcpy之外,C运行时库还具有memmove来处理这种重叠问题。

I assume std::copy works like memcpy , in that it doesn't pay any regard to overlap between the source and destination regions. 我假设std::copy工作方式与memcpy类似,因为它不考虑源和目标区域之间的重叠。 If you try to shift objects "down" in a std::vector with std::copy , you'll corrupt the data. 如果您尝试使用std::copystd::vector “向下”移动对象,则会破坏数据。 Is there an STL algorithm analogue of memmove to handle situations like this? 是否有一个STL算法类似memmove来处理这样的情况? Or should I roll my own with reverse iterators? 或者我应该使用反向迭代器自己滚动?

It doesn't handle overlapping ranges iff the beginning of the output range overlaps with the input range. 如果输出范围的开头与输入范围重叠,则它不处理重叠范围。

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_backward (这要求您不要将输出范围的末尾与输入范围重叠)。

Preconditions for std::copy , prohibits an overlap: std::copy前提条件禁止重叠:

  • Prototype 原型

     template <class InputIterator, class OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); 
  • Preconditions 前提条件

    • [first, last) is a valid range. [first, last)是有效范围。
    • result is not an iterator within the range [first, last) . result不是[first, last)范围内的迭代器。
    • There is enough space to hold all of the elements being copied. 有足够的空间来容纳所有被复制的元素。 More formally, the requirement is that [result, result + (last - first)) is a valid range. 更正式地说,要求是[result, result + (last - first))是有效范围。 [1] [1]

It seems the most straight forward way would be to create a temporary vector of the range you want to copy: 似乎最直接的方法是创建要复制的范围的临时向量:

std::vector copiedRange( srcVecIterBegin, srcVecIterEnd);
std::copy( copiedRange.begin(), copiedRange.end(), srcVecIterCopyLocIter);

You can wrap this in a templated function that should be ably to do an overlapped using any container/iterator type. 你可以将它包装在一个模板化的函数中,该函数应该可以使用任何容器/迭代器类型进行重叠。

C++17 standard draft C ++ 17标准草案

The C++17 n4659 standard draft says: C ++ 17 n4659标准草案说:

28.6.1 "Copy": 28.6.1“复制”:

 template<class InputIterator, class OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); 

1 Requires: result shall not be in the range [first, last). 1要求:结果不应在[first,last]范围内。

2 Effects: Copies elements in the range [first, last) into the range [result, result + (last - first)) starting from first and proceeding to last. 2效果:将[first,last]范围内的元素复制到[first,result +(last - first))范围,从第一个开始到最后一个。

and: 和:

 template<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward( BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); 

17 Requires: result shall not be in the range (first, last]. 17要求:结果不在范围内(第一个,最后一个)。

18 Effects: Copies elements in the range [first, last) into the range [result - (last-first), result) starting from last - 1 and proceeding to first. 18效果:将[first,last]范围内的元素复制到[last - first],结果的范围中,从最后一个开始 - 1并继续到第一个。 (263) For each positive integer n <= (last - first), performs *(result - n) = *(last - n). (263)对于每个正整数n <=(last - first),执行*(result - n)= *(last - n)。

The note then explains when to use copy_backward : 然后该注释说明何时使用copy_backward

263) copy_backward should be used instead of copy when last is in the range [result - (last - first), result) 263)当last在范围[result - (last - first),result)时,应该使用copy_backward而不是copy

Therefore, there is no requirement of no overlap for those functions, and unlike for memcpy , the behavior of overlaps is clearly defined in the Effects sections. 因此,不要求这些函数不重叠,并且memcpy不同 ,重叠的行为在Effects部分中明确定义。

You just choose between them because you usually want std::copy for copying left and std::copy_backward for copying right. 您只需在它们之间进行选择,因为您通常希望std::copy用于复制left而std::copy_backward用于复制权限。

C++ also has a ranged version of std::move in <algorithm> which moves instead of copying. C ++还有一个远程版本的std::move in <algorithm> ,它移动而不是复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM