名稱空間
變體
操作

std::transform

來自 cppreference.com
< 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 不能有副作用 它們不能修改所涉及的範圍

[編輯] 另請參閱

範圍中的元素應用一元函式物件
(函式模板) [編輯]
對一個範圍的元素應用函式
(演算法函式物件)[編輯]