- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
标准库提供了std::copy
,可以看作是C的memcpy()
的泛化/类化。它还保持了 memcpy()
的要求,范围 [first
, last
) 与范围 [d_first
, d_first + std::distance(first, last)
);否则我们有未定义的行为。
我的问题:是否有 std::memmove
的通用版本(即不满足该要求且通常使用临时缓冲区实现)?如果不是,怎么会?
最佳答案
C++ 没有减少 memcpy
的替代品和 memmove
.
对于非重叠范围,通常为 std::copy
使用,但 std::copy_backwards
也可以使用。
对于重叠范围,您需要使用 std::copy
或 std::copy_backwards
取决于重叠的性质。
也适用于复制昂贵的对象 std::move
( <algorithm>
)和 std::move_backward
如果原始对象不需要保留其值,则可以分别使用。
template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );Copies the elements in the range, defined by [first, last), to anotherrange beginning at d_first.
Copies all elements in the range [first, last) starting from first andproceeding to last - 1. The behavior is undefined if d_first is withinthe range [first, last). In this case, std::copy_backward may be usedinstead.
Notes
When copying overlapping ranges, std::copy is appropriate when copyingto the left (beginning of the destination range is outside the sourcerange) while std::copy_backward is appropriate when copying to theright (end of the destination range is outside the source range).
template< class BidirIt1, class BidirIt2 >
BidirIt2 copy_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );Copies the elements from the range, defined by [first, last), toanother range ending at d_last. The elements are copied in reverseorder (the last element is copied first), but their relative order ispreserved.
The behavior is undefined if d_last is within (first, last]. std::copymust be used instead of std::copy_backward in that case.
Notes
When copying overlapping ranges, std::copy is appropriate when copying to the left (beginning of the destinationrange is outside the source range) while std::copy_backward isappropriate when copying to the right (end of the destination range isoutside the source range).
关于c++ - 是否有与 memmove 等效的标准库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68050977/
下面的代码片段显示了 memmove() 的实现。 void my_memmove(void* dest, const void* src, size_t size) { unsigned i
在 CLRS 第 2 章中有一个练习,询问是否可以将插入排序的最坏情况运行时间改进为 O(n lg n)。我看到了this question发现做不到。 最坏情况下的复杂性无法改善,但与单独移动数组元
我正在使用memmove(),但目标似乎正在覆盖源,或者也许我不明白覆盖是什么。我有一个 char 数组(目标),然后是一个指向目标的指针,该指针位于 vector 内部。 char destinat
根据 C11 N1570 standard draft : 7.24.2.2 “memmove 函数”: The memmove function copies n characters from t
移动 2D double 组时遇到问题。在下面的代码中,我有 双权重[wt][2] 我可以将 Weights[0][0] 移动到 Weights[0][1],但只能在要移动的字节数末尾添加“*2”。为
我正在尝试读取和处理二进制文件。由于其编写方式,我以字符数组的形式读取它。当我处理完数据后。我将剩余数据移至字符缓冲区的开头以避免缓冲区溢出。我正在为此使用 memmove,但这似乎会导致堆增长,这表
假设有一个指向浮点值数组的指针:float *source; 并且我们知道它的大小为 int sourcesize; 有一个已经实现的函数,它从 inputVec 添加一个元素到 souce 数组:
我正在使用 memmove 将 std::string 元素向右移动一位。目的地的第一个地点是唯一一个乱七八糟、堆满垃圾的地方。我使用 memmove 而不是 strcpy 因为我在需要模板的类中使用
假设我有一个这样的整数数组 #define MAX 5 int bar [MAX] = {0}; int foo [MAX] = {3,1,0,0,0}; 现在我想移动这个数组,使所有空条目都在左边,
我想使用返回结果字符串的函数连接两个字符串。它会是这样的: char *String_Concat (char *String_1, char *String_2) { char *Strin
memmove 并没有真正移动内存,不是吗?它只是将内存从一个区域复制到另一个区域,并允许这两个区域重叠。我问这个问题是因为我只是想知道为什么这个 fnc 以非常误导的方式被调用。 因为我知道当某物从
我正在阅读关于 memcpy 和 memmove 的 c++ 引用资料,它们似乎在做同样的事情,除了 memmove 有一个特定的think called(允许目的地和来源重叠)。 什么是重叠以及何时
标准库提供了std::copy,可以看作是C的memcpy()的泛化/类化。它还保持了 memcpy() 的要求,范围 [first, last) 与范围 [d_first , d_first + s
我编写了以下函数,将给定的完整路径拆分为目录、文件名和扩展名。 #include #include #include struct path_info { char *directory
我在 StackOverflow 上进行了搜索,但找不到我想要做的事情。我想将指针 Items 复制到指针 COPYTO。然后就可以调用COPYTO->x。 #include typedef str
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Bus error troubleshooting 要从字符串中删除重复项,这是我编写的程序: #include #
当我编写时,我的代码不会崩溃: char s[44] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; memmove(s, "asdf", 5); 但
我正在尝试移动可能重叠的内存,对 *src 和 *dst 使用负增量或正增量,并且不使用大型临时缓冲区。 我正在尝试为 memmove() 函数找到一个有效的替代方案,大致如下: smart_memm
我试图了解 memmove 是如何工作的。我举一个例子,我以这种方式在内存中保存数据。 Start at 0 First Memory Block(A) of size 10 Hence A->(0,
我有以下代码,我试图在其中创建一个长度为 7 字节的缓冲区,然后将网络字节顺序中的整数与缓冲区中的 char 数组一起放入,最后我不应该有 Null 字符我的 sprintf() 缓冲区,所以我使用
我是一名优秀的程序员,十分优秀!