Namespaces
Variants

std::fill

来自 zh.cppreference.net
 
 
算法库
受约束的算法和范围上的算法 (C++20)
受约束的算法,例如 ranges::copy、ranges::sort、...
排序和相关操作
划分操作
(C++11)    

排序操作
二分查找操作
(在划分的范围上)
集合操作 (在已排序的范围上)
合并操作 (在已排序的范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <algorithm>
template< class ForwardIt, class T >
void fill( ForwardIt first, ForwardIt last, const T& value );
(1) (直到 C++26)
(C++20 起为 constexpr)
template< class ForwardIt, class T = typename std::iterator_traits
                                         <ForwardIt>::value_type >
constexpr void fill( ForwardIt first, ForwardIt last,
                     const T& value );
(C++26 起)
template< class ExecutionPolicy, class ForwardIt, class T >
void fill( ExecutionPolicy&& policy,
           ForwardIt first, ForwardIt last, const T& value );
(2) (C++17 起)
(直到 C++26)
template< class ExecutionPolicy,
          class ForwardIt, class T = typename std::iterator_traits
                                         <ForwardIt>::value_type >
void fill( ExecutionPolicy&& policy,
           ForwardIt first, ForwardIt last, const T& value );
(C++26 起)
1) 将给定的 value 赋值给范围 [first, last) 中的所有元素。
2) 与 (1) 相同,但按照 policy 执行。
只有当以下表达式的值为 true 时,此重载才参与重载决议:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(直到 C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20 起)

如果 value 不可 写入到 first,则程序非良构。

参数

first, last - 定义要修改的元素范围的迭代器对
value - 要赋的值
policy - 要使用的执行策略
类型要求
-
ForwardIt 必须满足 老式向前迭代器 (LegacyForwardIterator) 的要求。

复杂度

恰好 std::distance(first, last) 次赋值。

异常

带有模板参数名为 ExecutionPolicy 的重载按如下方式报告错误:

  • 如果作为算法一部分调用的函数执行时抛出异常,且 ExecutionPolicy 是 标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc。

可能的实现

fill (1)
template<class ForwardIt,
         class T = typename std::iterator_traits<ForwardIt>::value_type>
void fill(ForwardIt first, ForwardIt last, const T& value)
{
    for (; first != last; ++first)
        *first = value;
}

注解

功能特性测试宏 值 标准 功能
__cpp_lib_algorithm_default_value_type 202403 (C++26) 列表初始化用于算法 (1,2)

示例

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>

void println(const auto& seq)
{
    for (const auto& e : seq)
        std::cout << e << ' ';
    std::cout << '\n';
}

int main()
{
    std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8};
    println(v);
    
    // set all of the elements to 8
    std::fill(v.begin(), v.end(), 8);
    println(v);

    std::vector<std::complex<double>> nums{{1, 3}, {2, 2}, {4, 8}};
    println(nums);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::fill(nums.begin(), nums.end(), {4, 2});
    #else
        std::fill(nums.begin(), nums.end(), std::complex<double>{4, 2});
    #endif
    println(nums);
}

输出:

0 1 2 3 4 5 6 7 8
8 8 8 8 8 8 8 8 8
(1,3) (2,2) (4,8) 
(4,2) (4,2) (4,2)

缺陷报告

以下行为变更的缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 发布时的行为 正确的行为
LWG 283 C++98 T 要求为 可复制赋值,但
T 并不总是可写入 ForwardIt
改为要求可写入

参阅

向范围内的 N 个元素复制赋值给定值
(函数模板 & 算法函数对象)
将元素范围复制到新位置
(函数模板 & 算法函数对象)
将连续函数调用的结果赋值给范围内的每个元素
(函数模板 & 算法函数对象)
将函数应用于元素范围,并将结果存储在目标范围中
(函数模板 & 算法函数对象)
将范围内的元素赋值为某个值
(算法函数对象)