Namespaces
Variants

std:: fill

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
(1)
template < class ForwardIt, class T >
void fill ( ForwardIt first, ForwardIt last, const T & value ) ;
(自 C++20 起为 constexpr)
(直至 C++26)
template < class ForwardIt, class T = typename std:: iterator_traits

< ForwardIt > :: value_type >
constexpr void fill ( ForwardIt first, ForwardIt last,

const T & value ) ;
(自 C++26 起)
(2)
template < class ExecutionPolicy, class ForwardIt, class T >

void fill ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, const T & value ) ;
(自 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 执行。
此重载仅在满足以下所有条件时参与重载决议:

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

(C++20 前)

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

(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);
    // 将所有元素设置为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 被要求满足 CopyAssignable ,但
T 并不总是可写入 ForwardIt
改为要求可写入

参见

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