Namespaces
Variants

std::ranges:: for_each_n, std::ranges:: for_each_n_result

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 < std:: input_iterator I, class Proj = std:: identity ,

std:: indirectly_unary_invocable < std :: projected < I, Proj >> Fun >
constexpr for_each_n_result < I, Fun >

for_each_n ( I first, std:: iter_difference_t < I > n, Fun f, Proj proj = { } ) ;
(1) (C++20 起)
辅助类型
template < class I, class F >
using for_each_n_result = ranges:: in_fun_result < I, F > ;
(2) (C++20 起)
1) 将给定的函数对象 f 应用于通过 proj 投影的解引用结果,该投影作用于范围 [ first , first + n ) 中的每个迭代器,按顺序执行。

如果迭代器类型是可变的, f 可以通过解引用迭代器修改范围内的元素。若 f 返回结果,该结果将被忽略。如果 n 小于零,则行为未定义。

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

目录

参数

first - 指向待处理范围起始位置的迭代器
n - 待处理的元素数量
f - 应用于投影范围的函数 [ first , first + n )
proj - 应用于元素的投影

返回值

一个对象 { first + n, std :: move ( f ) } ,其中 first + n 可根据迭代器类别被求值为 std :: ranges:: next ( std :: move ( first ) , n )

复杂度

恰好应用 n f proj

可能的实现

struct for_each_n_fn
{
    template<std::input_iterator I, class Proj = std::identity,
             std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>
    constexpr for_each_n_result<I, Fun>
        operator()(I first, std::iter_difference_t<I> n, Fun fun, Proj proj = Proj{}) const
    {
        for (; n-- > 0; ++first)
            std::invoke(fun, std::invoke(proj, *first));
        return {std::move(first), std::move(fun)};
    }
};
inline constexpr for_each_n_fn for_each_n {};

示例

#include <algorithm>
#include <array>
#include <iostream>
#include <ranges>
#include <string_view>
struct P
{
    int first;
    char second;
    friend std::ostream& operator<<(std::ostream& os, const P& p)
    {
        return os << '{' << p.first << ",'" << p.second << "'}";
    }
};
auto print = [](std::string_view name, auto const& v)
{
    std::cout << name << ": ";
    for (auto n = v.size(); const auto& e : v)
        std::cout << e << (--n ? ", " : "\n");
};
int main()
{
    std::array a {1, 2, 3, 4, 5};
    print("a", a);
    // 对前三个数字取反:
    std::ranges::for_each_n(a.begin(), 3, [](auto& n) { n *= -1; });
    print("a", a);
    std::array s { P{1,'a'}, P{2, 'b'}, P{3, 'c'}, P{4, 'd'} };
    print("s", s);
    // 使用投影对数据成员 'P::first' 取反:
    std::ranges::for_each_n(s.begin(), 2, [](auto& x) { x *= -1; }, &P::first);
    print("s", s);
    // 使用投影将数据成员 'P::second' 大写化:
    std::ranges::for_each_n(s.begin(), 3, [](auto& c) { c -= 'a'-'A'; }, &P::second);
    print("s", s);
}

输出:

a: 1, 2, 3, 4, 5
a: -1, -2, -3, 4, 5
s: {1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'a'}, {-2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'A'}, {-2,'B'}, {3,'C'}, {4,'d'}

参见

范围 for 循环 (C++11) 在范围上执行循环
范围 中的元素应用一元 函数对象
(算法函数对象)
(C++17)
对序列的前N个元素应用函数对象
(函数模板)
范围 中的元素应用一元 函数对象
(函数模板)