Namespaces
Variants

std::ranges:: transform, std::ranges:: unary_transform_result, std::ranges:: binary_transform_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, std:: sentinel_for < I > S, std:: weakly_incrementable O,

std:: copy_constructible F, class Proj = std:: identity >
requires std:: indirectly_writable < O,
std:: indirect_result_t < F & , std :: projected < I, Proj >>>
constexpr unary_transform_result < I, O >

transform ( I first1, S last1, O result, F op, Proj proj = { } ) ;
(1) (C++20 起)
template < ranges:: input_range R, std:: weakly_incrementable O,

std:: copy_constructible F, class Proj = std:: identity >
requires std:: indirectly_writable < O,
std:: indirect_result_t < F & , std :: projected < ranges:: iterator_t < R > , Proj >>>
constexpr unary_transform_result < ranges:: borrowed_iterator_t < R > , O >

transform ( R && r, O result, F op, Proj proj = { } ) ;
(2) (C++20 起)
template < std:: input_iterator I1, std:: sentinel_for < I1 > S1,

std:: input_iterator I2, std:: sentinel_for < I2 > S2,
std:: weakly_incrementable O,
std:: copy_constructible F,
class Proj1 = std:: identity , class Proj2 = std:: identity >
requires std:: indirectly_writable < O,
std:: indirect_result_t < F & ,
std :: projected < I1, Proj1 > ,
std :: projected < I2, Proj2 >>>
constexpr binary_transform_result < I1, I2, O >
transform ( I1 first1, S1 last1, I2 first2, S2 last2, O result,

F binary_op, Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(3) (C++20 起)
template < ranges:: input_range R1,

ranges:: input_range R2,
std:: weakly_incrementable O,
std:: copy_constructible F,
class Proj1 = std:: identity , class Proj2 = std:: identity >
requires std:: indirectly_writable < O,
std:: indirect_result_t < F & ,
std :: projected < ranges:: iterator_t < R1 > , Proj1 > ,
std :: projected < ranges:: iterator_t < R2 > , Proj2 >>>
constexpr binary_transform_result < ranges:: borrowed_iterator_t < R1 > ,
ranges:: borrowed_iterator_t < R2 > , O >
transform ( R1 && r1, R2 && r2, O result, F binary_op,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(4) (C++20 起)
辅助类型
template < class I, class O >
using unary_transform_result = ranges:: in_out_result < I, O > ;
(5) (C++20 起)
template < class I1, class I2, class O >
using binary_transform_result =</span

将给定函数应用于某个范围,并将结果存储在始于 result 的另一范围中。

1) 一元操作 op 被应用于由 [ first1 , last1 ) 定义的区间(在使用投影 proj 进行投影之后)。
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,并使用 ranges:: end ( r ) 作为 last
3) 二元操作 binary_op 被应用于两个范围内的元素对:一个由 [ first1 , last1 ) 定义,另一个由 [ first2 , last2 ) 定义(在分别通过投影函数 proj1 proj2 进行投影之后)。
4) (3) 相同,但使用 r1 作为第一源范围,如同使用 ranges:: begin ( r1 ) 作为 first1 ranges:: end ( r1 ) 作为 last1 ,对于 r2 同理。

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

目录

参数

first1, last1 - 定义待转换第一个 范围 元素的迭代器-哨位对
r, r1 - 待转换的第一个元素范围
first2, last2 - 定义待转换第二个 范围 元素的迭代器-哨位对
r2 - 待转换的第二个元素范围
result - 目标范围的起始位置,可等于 first1 first2
op, binary_op - 应用于投影元素的操作
proj1 - 应用于第一个范围元素的投影
proj2 - 应用于第二个范围元素的投影

返回值

1,2) 一个 unary_transform_result 包含一个等于 last 的输入迭代器,以及一个指向最后一个被转换元素之后位置的输出迭代器。
3,4) 一个 binary_transform_result 包含指向范围 [ first1 , last1 ) [ first2 , last2 ) 中最后被转换元素的输入迭代器,分别作为 in1 in2 ,以及指向最后一个被转换元素之后位置的输出迭代器作为 out

复杂度

1,2) 恰好应用 op proj ranges:: distance ( first1, last1 ) 次。
3,4) 恰好 ranges:: min ( ranges:: distance ( first1, last1 ) , ranges:: distance ( first2, last2 ) ) 次对 binary_op 和投影函数的调用。

可能的实现

struct transform_fn
{
    // 第一个版本
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
             std::copy_constructible F, class Proj = std::identity>
    requires std::indirectly_writable<O, std::indirect_result_t<F&,
                                                                std::projected<I, Proj>>>
    constexpr ranges::unary_transform_result<I, O>
        operator()(I first1, S last1, O result, F op, Proj proj = {}) const
    {
        for (; first1 != last1; ++first1, (void)++result)
            *result = std::invoke(op, std::invoke(proj, *first1));
        return {std::move(first1), std::move(result)};
    }
    // 第二个版本
    template<ranges::input_range R, std::weakly_incrementable O,
             std::copy_constructible F, class Proj = std::identity>
    requires std::indirectly_writable<O,
                 std::indirect_result_t<F&, std::projected<ranges::iterator_t<R>, Proj>>>
    constexpr ranges::unary_transform_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, F op, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
                       std::move(op), std::move(proj));
    }
    // 第三个版本
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::input_iterator I2, std::sentinel_for<I2> S2,
             std::weakly_incrementable O,
             std::copy_constructible F,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires std::indirectly_writable<O,
                 std::indirect_result_t<F&,
                                        std::projected<I1, Proj1>,
                                        std::projected<I2, Proj2>>>
    constexpr ranges::binary_transform_result<I1, I2, O>
        operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result,
                   F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        for (; first1 != last1 && first2 != last2;
             ++first1, (void)++first2, (void)++result)
            *result = std::invoke(binary_op,
                                  std::invoke(proj1, *first1),
                                  std::invoke(proj2, *first2));
        return {std::move(first1), std::move(first2), std::move(result)};
    }
    // 第四个版本
    template<ranges::input_range R1, ranges::input_range R2,
             std::weakly_incrementable O, std::copy_constructible F,
             class Proj1 = std::identity, class Proj2 = std::

注释

ranges::transform 不保证按顺序应用 op binary_op 。若需按顺序对序列应用函数,或应用会修改序列元素的函数,请使用 ranges::for_each

示例

以下代码使用 ranges::transform 通过 std::toupper 函数将字符串就地转换为大写,然后将每个 char 转换为其序数值。 接着使用带投影的 ranges::transform std:: vector < Foo > 的元素转换为字符以填充 std::string

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
int main()
{
    std::string s{"hello"};
    auto op = [](unsigned char c) -> unsigned char { return std::toupper(c); };
    namespace ranges = std::ranges;
    // 就地大写化字符串
    ranges::transform(s.begin(), s.end(), s.begin(), op );
    std::vector<std::size_t> ordinals;
    // 将每个 char 转换为 size_t
    ranges::transform(s, std::back_inserter(ordinals),
                      [](unsigned char c) -> std::size_t { return c; });
    std::cout << s << ':';
    for (auto ord : ordinals)
        std::cout << ' ' << ord;
    // 将每个序数值翻倍
    ranges::transform(ordinals, ordinals, ordinals.begin(), std::plus{});
    std::cout << '\n';
    for (auto ord : ordinals)
        std::cout << ord << ' ';
    std::cout << '\n';
    struct Foo { char bar; };
    const std::vector<Foo> f = {{'h'},{'e'},{'l'},{'l'},{'o'}};
    std::string result;
    // 投影后大写化
    ranges::transform(f, std::back_inserter(result), op, &Foo::bar);
    std::cout << result << '\n';
}

输出:

HELLO: 72 69 76 76 79
144 138 152 152 158
HELLO

参见

范围 中的元素应用一元 函数对象
(算法函数对象)
对每个元素应用转换函数的序列 view
(类模板) (范围适配器对象)
对元素范围应用函数,将结果存储到目标范围中
(函数模板)