名前空間
変種
操作

std::transform

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
transform
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(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].
  • std::distance(first1, last1) + 1個の要素からなる範囲、d_firstから始まります。
3) 二項演算子binary_opは、2つの範囲からの要素のペアに適用されます:[first1last1)と、std::distance(first1, last1)個の要素からなる、first2から始まる別の範囲です。
binary_opがイテレータを無効にしたり、以下のいずれかの範囲の要素を変更したりした場合、動作は未定義となります。
  • [first1last1].
  • std::distance(first1, last1) + 1個の要素からなる範囲、first2から始まります。
  • std::distance(first1, last1) + 1個の要素からなる範囲、d_firstから始まります。
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 - 変換する2番目の範囲の開始位置。(3,4のみ)
d_first - 出力範囲の開始位置。first1またはfirst2と等しくても構いません。
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は、InputIt1型とInputIt2型のオブジェクトをそれぞれ間接参照して、 Type1 Type2に暗黙的に変換できるような型でなければなりません。型Retは、OutputIt型のオブジェクトを間接参照して、Ret型の値に代入できるような型でなければなりません。​

型要件
-
InputIt, InputIt1, InputIt2は、LegacyInputIteratorの要件を満たす必要があります。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2, ForwardIt3LegacyForwardIterator の要件を満たしている必要があります。

[編集] 戻り値

変換された最後の要素の次の要素を指す出力イテレータ。

[編集] 計算量

Nstd::distance(first1, last1)とすると

1,2) unary_opの適用はちょうどN回。
3,4) binary_opの適用はちょうどN回。

[編集] 例外

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_opまたはbinary_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_opおよびbinary_opは副作用を持つことはできません。 それらは関係する範囲を変更することはできません。

[編集] 関連項目

範囲内の要素に単項関数オブジェクトを適用する
(関数テンプレート) [編集]
要素の範囲に関数を適用する
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)