跳到主要内容

std::fill_n() 算法

// (1)
template< class OutputIt, class Size, class T >
constexpr OutputIt fill_n( OutputIt first, Size count, const T& value );

// (2)
template< class ExecutionPolicy, class ForwardIt, class Size, class T >
ForwardIt fill_n( ExecutionPolicy&& policy,
ForwardIt first, Size count, const T& value );
  • (1) 如果 count > 0,则将给定值赋值给从 first 开始的范围内前 count 个元素。
    否则不执行任何操作。

  • (2)(1),但根据策略执行。

    重载决议

    这些重载只有当 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (直到 C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (自 C++20 起)true 时才参与重载决议。

参数

first

要修改的元素范围。

count

要修改的元素数量。

要赋的值。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

类型要求

OutputItOutputIterator
LegacyForwardIteratorLegacyForwardIterator
Size

必须可转换为整型

必须可写入 first

返回值

如果 count > 0,则为最后一个已赋值元素之后一个位置的迭代器,否则为 first (自 C++11 起)
(无) (直到 C++11)

复杂度

恰好进行 std::max(0, count) 次赋值。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

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

可能的实现

fill_n (1)
template<class OutputIt, class Size, class T>
OutputIt fill_n(OutputIt first, Size count, const T& value)
{
for (Size i = 0; i < count; i++)
*first++ = value;
return first;
}

示例

以下代码使用fill_n()-1 赋值给整数向量的前半部分

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
std::vector<int> v1 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

std::fill_n(v1.begin(), 5, -1);

std::copy(begin(v1), end(v1), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
输出
-1 -1 -1 -1 -1 5 6 7 8 9
本文档源自此 CppReference 页面。它可能已被修改以进行改进或适应编辑者偏好。点击“编辑此页面”可查看对本文档进行的所有更改。
悬停查看原始许可证。

std::fill_n() 算法

// (1)
template< class OutputIt, class Size, class T >
constexpr OutputIt fill_n( OutputIt first, Size count, const T& value );

// (2)
template< class ExecutionPolicy, class ForwardIt, class Size, class T >
ForwardIt fill_n( ExecutionPolicy&& policy,
ForwardIt first, Size count, const T& value );
  • (1) 如果 count > 0,则将给定值赋值给从 first 开始的范围内前 count 个元素。
    否则不执行任何操作。

  • (2)(1),但根据策略执行。

    重载决议

    这些重载只有当 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (直到 C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (自 C++20 起)true 时才参与重载决议。

参数

first

要修改的元素范围。

count

要修改的元素数量。

要赋的值。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

类型要求

OutputItOutputIterator
LegacyForwardIteratorLegacyForwardIterator
Size

必须可转换为整型

必须可写入 first

返回值

如果 count > 0,则为最后一个已赋值元素之后一个位置的迭代器,否则为 first (自 C++11 起)
(无) (直到 C++11)

复杂度

恰好进行 std::max(0, count) 次赋值。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

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

可能的实现

fill_n (1)
template<class OutputIt, class Size, class T>
OutputIt fill_n(OutputIt first, Size count, const T& value)
{
for (Size i = 0; i < count; i++)
*first++ = value;
return first;
}

示例

以下代码使用fill_n()-1 赋值给整数向量的前半部分

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
std::vector<int> v1 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

std::fill_n(v1.begin(), 5, -1);

std::copy(begin(v1), end(v1), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
输出
-1 -1 -1 -1 -1 5 6 7 8 9
本文档源自此 CppReference 页面。它可能已被修改以进行改进或适应编辑者偏好。点击“编辑此页面”可查看对本文档进行的所有更改。
悬停查看原始许可证。