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

Does std::copy handle overlapping ranges?(std::copy 是否处理重叠范围?)
本文介绍了std::copy 是否处理重叠范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据从一个范围复制到另一个范围时,您必须小心源范围和目标范围之间是否存在部分重叠.如果目标范围的开头与源范围的尾部重叠,则纯顺序副本将使数据出现乱码.C 运行时库除了 memcpy 之外还有 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::copymemcpy 一样工作,因为它不考虑源区域和目标区域之间的重叠.如果您尝试使用 std::copystd::vector 中向下"移动对象,则会损坏数据.是否有类似 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?

推荐答案

如果输出范围的开头与输入范围重叠,则不处理重叠范围.

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 是否处理重叠范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)