简体   繁体   English

fill_n vs memset 设置数组的默认值

[英]fill_n vs memset to set default value of an array

I wonder what is the ideal way if you want to fill an array with a default value n:我想知道如果你想用默认值 n 填充数组,理想的方法是什么:

#include <cstring> // for memset
#include <algorithm> // for fill_n

static constexpr int N = 100;
int a[N];
static constexpr int defaultValue = -1;

void* memset( void* dest, int ch, std::size_t count );

memset(a, defaultValue, sizeof(a));

(memset) converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest. (memset) 将值 ch 转换为 unsigned char 并将其复制到 dest 指向的 object 的每个第一个 count 字符中。 If the object is a potentially-overlapping subobject or is not TriviallyCopyable (eg, scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined.如果 object 是潜在重叠的子对象或不是 TriviallyCopyable(例如,标量、C 兼容结构或可简单复制类型的数组),则行为未定义。 If count is greater than the size of the object pointed to by dest, the behavior is undefined.如果 count 大于 dest 指向的 object 的大小,则行为未定义。

or或者

constexpr OutputIt fill_n( OutputIt first, Size count, const T& value );

fill_n(a, N, defaultValue);

(fill_n) assigns the given value to the first count elements in the range beginning at first if count > 0. Does nothing otherwise. (fill_n) 如果 count > 0,则将给定值分配给从 first 开始的范围内的第一个 count 元素。否则不执行任何操作。

I am looking for insights, I know how to read the documentation of course!我正在寻找见解,我当然知道如何阅读文档!

edit: defaultValue might not be only -1.编辑:defaultValue 可能不仅仅是-1。

Both functions do different things.这两个函数做不同的事情。 Sure, they fill a block of memory, but the way they do it is completely different.当然,他们填充了一块 memory,但他们这样做的方式完全不同。

memset operates at the byte level. memset在字节级别运行。 defaultValue is hacked down to an unsigned char , so a defaultValue greater than what can fit into a single byte gets cut down to size and information is lost. defaultValue被破解为unsigned char ,因此大于可以容纳单个字节的defaultValue被缩减为大小并且信息丢失。 The now-byte-sized value is applied individually to every byte, not every int in the array.现在字节大小的值单独应用于每个字节,而不是数组中的每个int In the case of -1 you get "lucky" because four bytes worth of 0xFF looks the same, 0xFFFFFFFF, as a two's compliment -1 in the world of 32-bit integers.在 -1 的情况下,您会得到“幸运”,因为 0xFF 的四个字节看起来相同,即 0xFFFFFFFF,作为 32 位整数世界中的二进制补码 -1。 No such luck for most other numbers.大多数其他数字没有这样的运气。 1, for example, will not result in an array full of int 's set to 1, it's filled with 0x01010101, or 16843009.例如,1 不会导致一个充满int的数组设置为 1,而是用 0x01010101 或 16843009 填充。

fill_n , on the other hand, respects the array element's type.另一方面, fill_n尊重数组元素的类型。 Every int in the array will be set to defaultValue .数组中的每个int都将设置为defaultValue in the case of a defaultValue of 1, the array will be full of 1s.defaultValue为 1 的情况下,数组将充满 1。 defaultValue of 256, provides an array full of 256. defaultValue为 256,提供一个 256 的数组。

In terms of speed, it probably won't matter much.就速度而言,这可能并不重要。 Memory read or written in bytes is a rare sight these days.如今,以字节读取或写入的 Memory 已很少见。 Writing whole int s at a time may be faster.一次写入整个int可能会更快。 But a good memset implementation knows this and will be exploiting it.但是一个好的memset实现知道这一点并将利用它。 If it doesn't, the compiler likely will.如果没有,编译器可能会。

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

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