Namespaces
Variants

std:: for_each_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
for_each_n
(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>
template < class InputIt, class Size, class UnaryFunc >
InputIt for_each_n ( InputIt first, Size n, UnaryFunc f ) ;
(1) (C++17 起)
(C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt, class Size, class UnaryFunc >
ForwardIt for_each_n ( ExecutionPolicy && policy,

ForwardIt first, Size n, UnaryFunc f ) ;
(2) (C++17 起)

对范围 [ first , first + n ) 内每个迭代器解引用后的结果应用给定的函数对象 f 。若 f 返回结果,该结果将被忽略。

1) f 按顺序从 first 开始依次应用。
如果 UnaryFunc 不满足 可移动构造 (MoveConstructible) 要求,则行为未定义。
2) f 可能不会按顺序应用。该算法根据 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 起)
如果 UnaryFunc 不满足 可复制构造 要求,则行为未定义。

如果 n >= 0 不为 true ,则行为是未定义的。

如果迭代器类型( InputIt / ForwardIt )是可变的, f 可能通过解引用迭代器来修改范围内的元素。

与其他并行算法不同, for_each_n 不允许复制序列中的元素,即使它们是 TriviallyCopyable 的。

目录

参数

first - 要应用函数的范围起始位置
n - 要应用函数的元素数量
policy - 使用的 执行策略
f - 函数对象,将应用于范围 [ first , first + n ) 内每个迭代器解引用结果

函数签名应等价于:

void fun ( const Type & a ) ;

签名无需包含 const &
类型 Type 必须满足: InputIt 类型的对象在解引用后可隐式转换为 Type

类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
Size 必须可转换为整数类型。

返回值

一个等于 first + n 的迭代器,或更正式地说,等于 std:: advance ( first, n ) 的迭代器。

复杂度

恰好应用 f 函数 n 次。

异常

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

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

可能的实现

另请参阅 libstdc++ libc++ MSVC stdlib 中的实现。

template<class InputIt, class Size, class UnaryFunc>
InputIt for_each_n(InputIt first, Size n, UnaryFunc f)
{
    for (Size i = 0; i < n; ++first, (void) ++i)
        f(*first);
    return first;
}

示例

#include <algorithm>
#include <iostream>
#include <vector>
void println(auto const& v)
{
    for (auto count{v.size()}; const auto& e : v)
        std::cout << e << (--count ? ", " : "\n");
}
int main()
{
    std::vector<int> vi{1, 2, 3, 4, 5};
    println(vi);
    std::for_each_n(vi.begin(), 3, [](auto& n) { n *= 2; });
    println(vi);
}

输出:

1, 2, 3, 4, 5
2, 4, 6, 4, 5

参见

对范围内的元素应用函数,并将结果存储到目标范围
(函数模板)
range- for 循环 (C++11) 在范围上执行循环
对来自 范围 的元素应用一元 函数对象
(函数模板)
对序列的前N个元素应用函数对象
(算法函数对象)