memset & fill

memset

头文件

1
2
3
4
// c
#include <string.h>
// c++
#include <cstring>

使用

1
2
3
4
5
6
void *memset( void *dest, int ch, size_t count );
dest : 需要填充内存指针
ch : 填充内容,被强转成unsigned char使用
count: 需要填充数量

dest指针开始前count个字节的每个字节赋值为ch

示例

1
2
3
4
5
6
char str[] = "hello world";
memset (str, '-', 5);
puts(str);

// output
"----- world"

fill

头文件

1
#include <algorithm>

使用

1
2
3
4
5
template< class ForwardIt, class T >
constexpr void fill( ForwardIt first, ForwardIt last, const T& value );
first : 开始迭代器
last : 中止迭代器
value : 需要填充的值