命名空间
变体
操作

std::transform

来自 cppreference.cn
< cpp‎ | algorithm
 
 
算法库
约束算法和范围上的算法 (C++20)
约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作
(在已划分范围上)
集合操作 (在已排序范围上)
合并操作 (在已排序范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <algorithm>
template< class InputIt, class OutputIt, class UnaryOp >

OutputIt transform( InputIt first1, InputIt last1,

                    OutputIt d_first, UnaryOp unary_op );
(1) (constexpr since C++20)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class UnaryOp >
ForwardIt2 transform( ExecutionPolicy&& policy,
                      ForwardIt1 first1, ForwardIt1 last1,

                      ForwardIt2 d_first, UnaryOp unary_op );
(2) (since C++17)
template< class InputIt1, class InputIt2,

          class OutputIt, class BinaryOp >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2,

                    OutputIt d_first, BinaryOp binary_op );
(3) (constexpr since C++20)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2,
          class ForwardIt3, class BinaryOp >
ForwardIt3 transform( ExecutionPolicy&& policy,
                      ForwardIt1 first1, ForwardIt1 last1,
                      ForwardIt2 first2,

                      ForwardIt3 d_first, BinaryOp binary_op );
(4) (since C++17)

std::transform 将给定的函数应用于给定输入范围的元素,并将结果存储在从 d_first 开始的输出范围中。

1) 一元操作 unary_op 应用于 [first1last1) 的元素。
如果 unary_op 使迭代器失效或修改了以下任何范围内的元素,则行为未定义
  • [first1last1].
  • d_first 开始的 std::distance(first1, last1) + 1 个元素的范围。
3) 二元操作 binary_op 应用于来自两个范围的元素对:[first1last1) 和另一个从 first2 开始的 std::distance(first1, last1) 个元素的范围。
如果 binary_op 使迭代器失效或修改了以下任何范围内的元素,则行为未定义
  • [first1last1].
  • first2 开始的 std::distance(first1, last1) + 1 个元素的范围。
  • d_first 开始的 std::distance(first1, last1) + 1 个元素的范围。
2,4)(1,3) 相同,但根据 policy 执行。
仅当满足以下所有条件时,这些重载才参与重载解析

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(until C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(since C++20)

内容

[编辑] 参数

first1, last1 - 定义要转换元素的源范围的迭代器对
first2 - 要转换的第二个元素范围的开始,仅限 (3,4)
d_first - 目标范围的开始,可能等于 first1first2
policy - 要使用的执行策略
unary_op - 将要应用的一元操作函数对象。

函数的签名应等效于以下内容

 Ret fun(const Type &a);

签名不需要具有 const &
类型  Type 必须是这样的类型:类型为 InputIt 的对象可以被解引用,然后隐式转换为  Type。类型 Ret 必须是这样的类型:类型为 OutputIt 的对象可以被解引用并赋值类型为 Ret 的值。 ​

binary_op - 将要应用的二元操作函数对象。

函数的签名应等效于以下内容

 Ret fun(const Type1 &a, const Type2 &b);

签名不需要具有 const &
类型  Type1 Type2 必须是这样的类型:类型为 InputIt1InputIt2 的对象可以被解引用,然后分别隐式转换为  Type1 Type2。类型 Ret 必须是这样的类型:类型为 OutputIt 的对象可以被解引用并赋值类型为 Ret 的值。 ​

类型要求
-
InputIt, InputIt1, InputIt2 必须满足 LegacyInputIterator 的要求。
-
OutputIt 必须满足 LegacyOutputIterator 的要求。
-
ForwardIt1, ForwardIt2, ForwardIt3 必须满足 LegacyForwardIterator 的要求。

[编辑] 返回值

指向最后一个转换元素的下一个位置的输出迭代器。

[编辑] 复杂度

给定 Nstd::distance(first1, last1)

1,2) 精确地 N 次应用 unary_op
3,4) 精确地 N 次应用 binary_op

[编辑] 异常

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

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

[编辑] 可能的实现

transform (1)
template<class InputIt, class OutputIt, class UnaryOp>
constexpr //< since C++20
OutputIt transform(InputIt first1, InputIt last1,
                   OutputIt d_first, UnaryOp unary_op)
{
    for (; first1 != last1; ++d_first, ++first1)
        *d_first = unary_op(*first1);
 
    return d_first;
}
transform (3)
template<class InputIt1, class InputIt2, 
         class OutputIt, class BinaryOp>
constexpr //< since C++20
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
                   OutputIt d_first, BinaryOp binary_op)
{
    for (; first1 != last1; ++d_first, ++first1, ++first2)
        *d_first = binary_op(*first1, *first2);
 
    return d_first;
}

[编辑] 注意

std::transform 不保证按顺序应用 unary_opbinary_op。要按顺序将函数应用于序列或应用修改序列元素的函数,请使用 std::for_each

[编辑] 示例

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
 
void print_ordinals(const std::vector<unsigned>& ordinals)
{
    std::cout << "ordinals: ";
    for (unsigned ord : ordinals)
        std::cout << std::setw(3) << ord << ' ';
    std::cout << '\n';
}
 
char to_uppercase(unsigned char c)
{
    return std::toupper(c);
}
 
void to_uppercase_inplace(char& c)
{
    c = to_uppercase(c);
}
 
void unary_transform_example(std::string& hello, std::string world)
{
    // Transform string to uppercase in-place
 
    std::transform(hello.cbegin(), hello.cend(), hello.begin(), to_uppercase);
    std::cout << "hello = " << std::quoted(hello) << '\n';
 
    // for_each version (see Notes above)
    std::for_each(world.begin(), world.end(), to_uppercase_inplace);
    std::cout << "world = " << std::quoted(world) << '\n';
}
 
void binary_transform_example(std::vector<unsigned> ordinals)
{
    // Transform numbers to doubled values
 
    print_ordinals(ordinals);
 
    std::transform(ordinals.cbegin(), ordinals.cend(), ordinals.cbegin(),
                   ordinals.begin(), std::plus<>{});
 
    print_ordinals(ordinals);
}
 
int main()
{
    std::string hello("hello");
    unary_transform_example(hello, "world");
 
    std::vector<unsigned> ordinals;
    std::copy(hello.cbegin(), hello.cend(), std::back_inserter(ordinals));
    binary_transform_example(std::move(ordinals));
}

输出

hello = "HELLO"
world = "WORLD"
ordinals:  72  69  76  76  79 
ordinals: 144 138 152 152 158

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 242 C++98 unary_opbinary_op 不能有副作用 它们不能修改所涉及的范围

[编辑] 参见

将一元函数对象应用于来自范围的元素
(函数模板) [编辑]
将函数应用于元素范围
(算法函数对象)[编辑]