algorithm头文件下的fill()

fill()可以把数组或容器中的某一段区间赋予相同的值。

和memset不同,这里可以是数组类型对应范围中的任意值

示例如下:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	int a[5]={1,2,3,4,5};
	fill(a+1,a+4,666);//将a[1]~a[3]赋值666
	for(int i=0;i<5;i++)
		cout<<a[i]<<" ";
	return 0; 
}

输出结果:

1 666 666 666 5 
发布了94 篇原创文章 · 获赞 193 · 访问量 5381

猜你喜欢

转载自blog.csdn.net/weixin_45884316/article/details/104198640