STL fill函数

STL fill函数算法

STL 中的 fill() 函数用于为元素序列填入给定值的简单方式,fill() 会填充整个序列。

STL fill函数详解

头文件

#include <algorithm>

语法

template<class ForwardIterator, class Type> void fill(ForwardIterator _First, ForwardIterator _Last, const Type& _Val);

参数

参数 描述
_First 输入迭代器
_Last 输入迭代器
_Val 要填充的值

案例

STL fill函数

使用 STL fill 函数填充元素

#include<iostream> #include<algorithm> #include <vector> using namespace std; int main() { std::cout << "嗨客网(www.haicoder.net)\n" << std::endl; std::vector<string> data {12}; std::fill(std::begin(data), std::end(data), "none"); for(auto i : data) { std::cout << i << " "; } std::cout << '\n'; return 0; }

我们在 Linux 下使用 g++ 进行编译,具体命令如下:

g++ fill.cpp -std=c++11

编译后,我们直接运行生成的二进制文件 a.out,如下图所示:

51_STL fill函数.png

我们使用了 fill 函数,实现了填充集合。

STL fill函数总结

STL 中的 fill() 函数用于为元素序列填入给定值的简单方式,fill() 会填充整个序列。