命名空间
变体
操作

std::transform

来自 cppreference.cn
< cpp‎ | 算法
 
 
算法库
有约束算法与针对范围的算法 (C++20)
有约束的算法,例如 ranges::copyranges::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) (C++20 起为 constexpr)
template< class ExecutionPolicy,

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

                      ForwardIt2 d_first, UnaryOp unary_op );
(2) (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) (C++20 起为 constexpr)
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) (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

(C++20 前)

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

(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 的要求。

[编辑] 返回值

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

[编辑] 复杂度

给定 N 作为 std::distance(first1, last1)

1,2) 精确地应用 Nunary_op
3,4) 精确地应用 Nbinary_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++ 标准。

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

[编辑] 另请参阅

范围中的元素应用一元函数对象
(函数模板) [编辑]
对一个范围的元素应用函数
(算法函数对象)[编辑]