功能描述:
向容器中填充指定的元素
函数原型:
fill(iterator beg,iterator end,value);
//向容器中填充元素
//beg 开始迭代器
//end 结束迭代器
//value 填充的值
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(0);
v.resize(10);
fill(v.begin(), v.end(), 1);
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出结果:
1 1 1 1 1 1 1 1 1 1
总结:利用fill可以将容器区间内元素填充为指定的值