Namespaces
Variants

std::ranges:: 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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
(1)
template < class T, std:: output_iterator < const T & > O, std:: sentinel_for < O > S >
constexpr O fill ( O first, S last, const T & value ) ;
(C++20 起)
(C++26 前)
template < class O, std:: sentinel_for < O > S, class T = std:: iter_value_t < O > >

requires std:: output_iterator < O, const T & >

constexpr O fill ( O first, S last, const T & value ) ;
(C++26 起)
(2)
template < class T, ranges:: output_range < const T & > R >
constexpr ranges:: borrowed_iterator_t < R > fill ( R && r, const T & value ) ;
(C++20 起)
(C++26 前)
template < class R, class T = ranges:: range_value_t < R > >

requires ranges:: output_range < R, const T & >

constexpr ranges:: borrowed_iterator_t < R > fill ( R && r, const T & value ) ;
(C++26 起)
1) 将给定的 value 赋值给范围 [ first , last ) 内的元素。
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,以及 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待修改元素范围的 区间 的迭代器-哨位对
r - 待修改的元素范围
value - 待赋值的值

返回值

一个输出迭代器,与 last 比较相等。

复杂度

恰好 last - first 次赋值。

可能的实现

struct fill_fn
{
    template<class O, std::sentinel_for<O> S, class T = std::iter_value_t<O>>
    requires std::output_iterator<O, const T&>
    constexpr O operator()(O first, S last, const T& value) const
    {
        while (first != last)
            *first++ = value;
        return first;
    }
    template<class R, class T = ranges::range_value_t<R>>
    requires ranges::output_range<R, const T&>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, const T& value) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value);
    }
};
inline constexpr fill_fn fill;

注释

功能测试 标准 功能
__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};
    // 使用重载(1)将所有元素设置为-1
    std::ranges::fill(v.begin(), v.end(), -1);
    println(v);
    // 使用重载(2)将所有元素设置为10
    std::ranges::fill(v, 10);
    println(v);
    std::vector<std::complex<double>> nums{{1, 3}, {2, 2}, {4, 8}};
    println(nums);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::ranges::fill(nums, {4, 2}); // T 被自动推导
    #else
        std::ranges::fill(nums, std::complex<double>{4, 2});
    #endif
    println(nums);
}

输出:

-1 -1 -1 -1 -1 -1
10 10 10 10 10 10
(1,3) (2,2) (4,8)
(4,2) (4,2) (4,2)

参见

为指定数量的元素赋值
(算法函数对象)
将元素范围复制到新位置
(算法函数对象)
将函数结果保存到范围中
(算法函数对象)
对元素范围应用函数
(算法函数对象)
使用均匀随机位生成器产生的随机数填充范围
(算法函数对象)
将给定值复制赋值给范围内的每个元素
(函数模板)