templateinline void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
对一个容器的值进行填充时
参数
first 和 last 都为正向迭代器,[first, last) 用于指定该函数的查找范围;
val 给定值;
fill 容器#include数组#include #include #include #include int main() { //容器 std::vector b;//没确定配容器大小,不分配内存 std::fill(std::begin(b), std::end(b), 4); std::cout << "b fill vector: "; std::for_each(std::begin(b), std::end(b), [](int value) { std::cout << value << ", "; }); std::cout << std::endl; std::vector a(10); std::fill(std::begin(a), std::end(a), 6); std::cout << "a fill vector: "; std::for_each(std::begin(a), std::end(a), [](int value) { std::cout << value << ", "; }); return -1; } //输出 b fill vector: a fill vector: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
#includefill_n#include #include #include #include int main() { 容器 //std::vector b;//没确定配容器大小,不分配内存 //std::fill(std::begin(b), std::end(b), 4); //std::cout << "b fill vector: "; //std::for_each(std::begin(b), std::end(b), [](int value) { // std::cout << value << ", "; //}); //std::cout << std::endl; //std::vector a(10); //一维 int a[8]{ 0 }; std::fill(std::begin(a), std::end(a), 6); std::cout << "a fill vector: "; std::for_each(std::begin(a), std::end(a), [](int value) { std::cout << value << ", "; }); std::cout << std::endl; //二维 int b[2][4]{ 0 }; std::fill(b[0], b[0] + 2 * 4, 4); std::cout << "b fill vector: "; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { std::cout << b[i][j] << ", "; } } std::cout << std::endl; return -1; } //输出 a fill vector: 6, 6, 6, 6, 6, 6, 6, 6, b fill vector: 4, 4, 4, 4,
函数原型
templateinline _OutIt fill_n(_OutIt _Dest, _Diff _Count_raw, const _Ty& _Val)
以给定的迭代器为起始位置,为指定个数的元素设置值。
#include#include #include #include #include #include int main() { //容器 std::vector b(5); std::fill_n(std::begin(b), b.size(), 4); std::cout << "b fill vector: "; std::for_each(std::begin(b), std::end(b), [](int value) { std::cout << value << ", "; }); std::cout << std::endl; //插入迭代器 std::vector d; std::fill_n(std::back_inserter(d), 5, 7); std::cout << "d fill vector: "; std::for_each(std::begin(d), std::end(d), [](int value) { std::cout << value << ", "; }); return -1; } //输出 b fill vector: 4, 4, 4, 4, 4, d fill vector: 7, 7, 7, 7, 7,



