【C++】fill函数,fill与memset函数的区别
转载自:https://blog.csdn.net/liuchuo/article/details/52296646
memset函数
按照字节填充某字符
在头文件<cstring>里面
fill函数
按照单元赋值,将一个区间的元素都赋同一个值
在头文件<algorithm>里面
因为memset函数按照字节填充,所以一般memset只能用来填充char型数组,(因为只有char型占一个字节)如果填充int型数组,除了0和-1,其他的不能。因为只有00000000 = 0,-1同理,如果我们把每一位都填充“1”,会导致变成填充入“11111111”
而fill函数可以赋值任何,而且使用方法特别简便:
例如int数组:fill(arr, arr + n, 要填入的内容);
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
int arr[10];
fill(arr, arr + 10, 2);
return 0;
}
1
2
3
4
5
6
7
8
vector也可以:fill(v.begin(), v.end(), 要填入的内容);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
fill(v.begin(), v.end(), -1);
return 0;
}
1
2
3
4
5
6
7
8
9
而memset的使用方法是:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
int a[20];
memset(a, 0, sizeof a);
return 0;
}
---------------------
作者:柳婼
来源:CSDN
原文:https://blog.csdn.net/liuchuo/article/details/52296646
版权声明:本文为博主原创文章,转载请附上博文链接!
【C++】fill函数,fill与memset函数的区别的更多相关文章
- c++ memset函数
函数名称:memset 函数所需头文件:#include<cstring> 函数作用:内存赋值函数,用来给某一块内存空间进行赋值的. 函数结构:memset(变量,一个数字,一个数字) ...
- 透彻分析C/C++中memset函数
在C语言中,经常需要对内存进行操作,里面涉及很多函数,但是memset函数的使用有一点需要大家格外注意,这也是我在做项目时遇到过的一个问题,调试了很久才找出来错误. 函数原型是:void *memse ...
- 如何给数组用fill函数和memset函数给数组赋初值
fill是按照单元来赋值的,所以可以填充一个区间的任意值 #include<iostream> #include<stdio.h> #include<string.h&g ...
- fill,fill-n,memset的区别
这里在网上搜集归纳了一个总结 memset函数 按照字节填充某字符 在头文件<string.h>中 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组,(因为 ...
- memset()函数的使用
1.概述 memset()函数,称为按字节赋值函数,使用时需要加头文件 #include<cstring>或者#include<string.h>.通常有两个用法: (1)用来 ...
- memset函数
函数介绍 void *memset(void *s, int ch, size_t n); 函数解释:将s中前n个字节 (typedef unsigned int size_t )用 ch 替换并返回 ...
- memset函数详解
语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关 功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...
- 深入学习 memset 函数
最近,和同学讨论了一下memset函数,趁着周五空闲做一总结. memset函数最常用的功能就是初始化数组了(主要是置零),如 #include <iostream> #include & ...
- strcpy, memcpy, memset函数
一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #inclu ...
随机推荐
- vscode配置golang
https://www.cnblogs.com/Leo_wl/p/8242628.html https://www.cnblogs.com/angelyan/p/10400789.html 主要看了这 ...
- offsetWidth clientWidth scrollWidth 的区别
了解 offsetWidth clientWidth scrollWidth 的区别 最近需要清除区分开元素的width,height及相应的坐标等,当前这篇用来区分offsetWidth clien ...
- 执行npm publish 报错:403 Forbidden - PUT https://registry.npmjs.org/kunmomotest - you must verify your email before publishing a new package: https://www.npmjs.com/email-edit
前言 执行npm publish 报错:403 Forbidden - PUT https://registry.npmjs.org/kunmomotest - you must verify you ...
- python-xss攻击和单例模式
1.python中单例模式 class Foo: instance = None def __new__(cls, *args, **kwargs): if not Foo.instance: Foo ...
- switch语句小练习
java有两钟选择判断语句,分别是if else和switch case语句. 下面我们做一个switch case语句的练习. // 定义一个扫描器 Scanner sacnner = new Sc ...
- mknod - 建立块专用或字符专用文件
总览 mknod [options] name {bc} major minor mknod [options] name p GNU 选项(缩写): [-m mode] [--help] [--ve ...
- 强化学习(Reinfment Learning) 简介
本文内容来自以下两个链接: https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/ https: ...
- HDU 5988 Coding Contest 最小费用流 cost->double
Problem Description A coding contest will be held in this university, in a huge playground. The whol ...
- element-ui 表格标题换行
render-header: 列标题 Label 区域渲染使用的 Function <template> <el-table :data="dataList"& ...
- linux 时区问题
1.java项目发现 服务器时间不正确,修改了服务器时间之后依然没解决. 2.java虚拟机的时区也需要设置: