Namespaces
Variants

std::ranges:: fill_n

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>
调用签名
template < class T, std:: output_iterator < const T & > O >
constexpr O fill_n ( O first, std:: iter_difference_t < O > n, const T & value ) ;
(C++20 起)
(C++26 前)
template < class O, class T = std:: iter_value_t < O > >

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

constexpr O fill_n ( O first, std:: iter_difference_t < O > n, const T & value ) ;
(C++26 起)

将给定的 value 赋值给范围 [ first , first + n ) 内的所有元素。

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

目录

参数

first - 要修改的元素范围的起始位置
n - 要修改的元素数量
value - 要赋值的值

返回值

一个输出迭代器,比较等于 first + n

复杂度

恰好 n 个赋值操作。

可能的实现

struct fill_n_fn
{
    template<class O, class T = std::iter_value_t<O>>
    requires std::output_iterator<O, const T&>
    constexpr O operator()(O first, std::iter_difference_t<O> n, const T& value) const
    {
        for (std::iter_difference_t<O> i {}; i != n; ++first, ++i)
            *first = value;
        return first;
    }
};
inline constexpr fill_n_fn fill_n {};

注释

功能测试 标准 功能
__cpp_lib_algorithm_default_value_type 202403 (C++26) 算法的列表初始化

示例

#include <algorithm>
#include <complex>
#include <iostream>
#include <string>
#include <vector>
void println(const auto& v)
{
    for (const auto& elem : v)
        std::cout << ' ' << elem;
    std::cout << '\n';
}
int main()
{
    constexpr auto n{8};
    std::vector<std::string> v(n, "▓▓░░");
    println(v);
    std::ranges::fill_n(v.begin(), n, "░░▓▓");
    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_n(nums.begin(), 2, {4, 2});
    #else
        std::ranges::fill_n(nums.begin(), 2, std::complex<double>{4, 2});
    #endif
    println(nums);
}

输出:

 ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░
 ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓
 (1,3) (2,2) (4,8)
 (4,2) (4,2) (4,8)

参见

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