std:: transform
|
ヘッダーで定義
<algorithm>
|
||
|
template
<
class
InputIt,
class
OutputIt,
class
UnaryOp
>
OutputIt transform
(
InputIt first1, InputIt last1,
|
(1) | (C++20以降constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
UnaryOp
>
|
(2) | (C++17以降) |
|
template
<
class
InputIt1,
class
InputIt2,
class
OutputIt,
class
BinaryOp
>
|
(3) | (C++20以降constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
|
(4) | (C++17以降) |
std::transform
は、指定された入力範囲の要素に関数を適用し、その結果を
d_first
から始まる出力範囲に格納します。
[
first1
,
last1
)
の要素に適用されます。
-
[first1,last1]. - std:: distance ( first1, last1 ) + 1 個の要素からなる範囲( d_first から開始)。
[
first1
,
last1
)
と、
std::
distance
(
first1, last1
)
個の要素を持つ
first2
から始まる別の範囲の要素ペアに適用されます。
-
[first1,last1]. - std:: distance ( first1, last1 ) + 1 個の要素から始まる first2 からの範囲。
- std:: distance ( first1, last1 ) + 1 個の要素から始まる d_first からの範囲。
|
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
&
は必要ない。
|
| binary_op | - |
適用される二項演算関数オブジェクト
関数のシグネチャは以下と同等であること: Ret fun ( const Type1 & a, const Type2 & b ) ;
シグネチャに
const
&
は必要ない。
|
| 型要件 | ||
-
InputIt, InputIt1, InputIt2
は
LegacyInputIterator
の要件を満たすこと
|
||
-
OutputIt
は
LegacyOutputIterator
の要件を満たすこと
|
||
-
ForwardIt1, ForwardIt2, ForwardIt3
は
LegacyForwardIterator
の要件を満たすこと
|
||
戻り値
変換された最後の要素に続く要素への出力イテレータ。
計算量
与えられた N を std:: distance ( first1, last1 ) として:
例外
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) { // 文字列を大文字に変換(インプレース) std::transform(hello.cbegin(), hello.cend(), hello.begin(), to_uppercase); std::cout << "hello = " << std::quoted(hello) << '\n'; // for_each バージョン(上記の注記を参照) 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) { // 数値を2倍の値に変換 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 は副作用を持つことができなかった | 関連する範囲を変更することはできない |
関連項目
|
単項
関数オブジェクト
を
範囲
の要素に適用する
(関数テンプレート) |
|
|
(C++20)
|
要素の範囲に関数を適用する
(アルゴリズム関数オブジェクト) |