概述
std::fill和std::replace是C++标准库中两个常用的算法函数,用于对容器中的元素进行批量操作。它们都定义在<algorithm>头文件中,提供了高效的元素赋值和替换功能。
std::fill 函数详解
基本语法
template< class ForwardIt, class T > void fill( ForwardIt first, ForwardIt last, const T& value );
功能说明
std::fill将指定范围内的所有元素都设置为给定的值。
参数说明
-
first, last:要填充的元素范围,包含first,但不包含last -
value:要设置的值
使用示例
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec(5); // 创建包含5个元素的vector
// 将整个vector填充为42
std:
订阅专栏 解锁全文
1349

被折叠的 条评论
为什么被折叠?



