namespace std::ranges {
template <input_iterator I,
sentinel_for<I> S,
weakly_incrementable O,
copy_constructible F,
class Proj = identity>
requires indirectly_writable<
O,
indirect_result_t<F&, projected<I, Proj>>
>
constexpr unary_transform_result<I, O>
transform(I first1,
S last1,
O result,
F op,
Proj proj = {}); // (1) C++20
template <input_range R,
weakly_incrementable O,
copy_constructible F,
class Proj = identity>
requires indirectly_writable<
O,
indirect_result_t<F&, projected<iterator_t<R>, Proj>>
>
constexpr unary_transform_result<borrowed_iterator_t<R>, O>
transform(R&& r,
O result,
F op,
Proj proj = {}); // (2) C++20
template <input_iterator I1,
sentinel_for<I1> S1,
input_iterator I2,
sentinel_for<I2> S2,
weakly_incrementable O,
copy_constructible F,
class Proj1 = identity,
class Proj2 = identity>
requires indirectly_writable<
O,
indirect_result_t<F&, projected<I1, Proj1>, projected<I2, Proj2>>
>
constexpr binary_transform_result<I1, I2, O>
transform(I1 first1,
S1 last1,
I2 first2,
S2 last2,
O result,
F binary_op,
Proj1 proj1 = {},
Proj2 proj2 = {}); // (3) C++20
template <input_range R1,
input_range R2,
weakly_incrementable O,
copy_constructible F,
class Proj1 = identity,
class Proj2 = identity>
requires indirectly_writable<
O,
indirect_result_t<F&, projected<iterator_t<R1>, Proj1>,
projected<iterator_t<R2>, Proj2>>
>
constexpr binary_transform_result<
borrowed_iterator_t<R1>,
borrowed_iterator_t<R2>,
O
>
transform(R1&& r1,
R2&& r2,
O result,
F binary_op,
Proj1 proj1 = {},
Proj2 proj2 = {}); // (4) C++20
template <execution-policy Ep,
random_access_iterator I,
sized_sentinel_for<I> S,
random_access_iterator O,
sized_sentinel_for<O> OutS,
copy_constructible F,
class Proj = identity>
requires indirectly_writable<
O,
indirect_result_t<F&, projected<I, Proj>>
>
unary_transform_result<I, O>
transform(Ep&& exec,
I first1,
S last1,
O result,
OutS result_last,
F op,
Proj proj = {}); // (5) C++26
template <execution-policy Ep,
sized-random-access-range R,
sized-random-access-range OutR,
copy_constructible F,
class Proj = identity>
requires indirectly_writable<
iterator_t<OutR>,
indirect_result_t<F&, projected<iterator_t<R>, Proj>>
>
unary_transform_result<borrowed_iterator_t<R>, borrowed_iterator_t<OutR>>
transform(Ep&& exec,
R&& r,
OutR&& result_r,
F op,
Proj proj = {}); // (6) C++26
template <execution-policy Ep,
random_access_iterator I1,
sized_sentinel_for<I1> S1,
random_access_iterator I2,
sized_sentinel_for<I2> S2,
random_access_iterator O,
sized_sentinel_for<O> OutS,
copy_constructible F,
class Proj1 = identity,
class Proj2 = identity>
requires indirectly_writable<
O,
indirect_result_t<F&, projected<I1, Proj1>, projected<I2, Proj2>>
>
binary_transform_result<I1, I2, O>
transform(Ep&& exec,
I1 first1,
S1 last1,
I2 first2,
S2 last2,
O result,
OutS result_last,
F binary_op,
Proj1 proj1 = {},
Proj2 proj2 = {}); // (7) C++26
template <execution-policy Ep,
sized-random-access-range R1,
sized-random-access-range R2,
sized-random-access-range OutR,
copy_constructible F,
class Proj1 = identity,
class Proj2 = identity>
requires indirectly_writable<
iterator_t<OutR>,
indirect_result_t<F&, projected<iterator_t<R1>, Proj1>,
projected<iterator_t<R2>, Proj2>>
>
binary_transform_result<
borrowed_iterator_t<R1>,
borrowed_iterator_t<R2>,
borrowed_iterator_t<OutR>
>
transform(Ep&& exec,
R1&& r1,
R2&& r2,
OutR&& result_r,
F binary_op,
Proj1 proj1 = {},
Proj2 proj2 = {}); // (8) C++26
}
概要
全ての要素に関数を適用する。
- (1), (2): 1つの範囲の要素に関数を適用し、結果を出力イテレータに出力する
- (3), (4): 2つの範囲の要素を1つずつ取り出して関数を適用し、結果を出力イテレータに出力する
- (5), (6): (1), (2)の並列アルゴリズム版。実行ポリシーを指定する
-
(7), (8): (3), (4)の並列アルゴリズム版。実行ポリシーを指定する
-
(1), (3): イテレータ範囲を指定する
- (2), (4): Rangeを直接指定する
事前条件
- (1), (2) :
opは、[first,last],[result,result + (last - first)]内のイテレータや subrange を無効にしたり、要素を書き換えてはならない。 - (3), (4) :
binary_opは、[first1,last1],[first2,first2 + (last1 - first1)],[result,result + (last1 - first1)]内のイテレータや subrange を無効にしたり、要素を書き換えてはならない。
※ 閉区間で表しているのは意図的なもの
効果
- (1), (2) :
[result,result + (last - first))内のイテレータiの要素に、それぞれop(*(first + (i - result)))を代入する - (3), (4) :
[result,result + (last1 - first1))内のイテレータiの要素に、それぞれbinary_op(*(first1 + (i - result)), *(first2 + (i - result)))を代入する。
戻り値
- (1), (2) :
{ .in = last, .out = result + (last - first) } - (3), (4) :
{ .in1 = last1, .in2 = last2, .out = result + (last1 - first1) }
計算量
- (1), (2) : 正確に
last - first回のopの適用が行われる。 - (3), (4) : 正確に
last1 - first1回のbinary_opの適用が行われる。
備考
- (1), (2) :
resultはfirstと同じであっても構わない。 - (3), (4) :
resultはfirst1やfirst2と同じであっても構わない。
例
(1)の例
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
int main() {
std::vector<int> v = { 3,1,4 };
std::vector<std::string> result;
// 2倍してから文字列に変換する
std::ranges::transform(v, std::back_inserter(result), [](int x) { return std::to_string(x * 2); });
for (const std::string& s : result) {
std::cout << s << std::endl;
}
}
出力
6
2
8
(2)の例
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
int main() {
std::vector<char> v1 = { 'a','b','c' };
std::vector<int> v2 = { 3,1,4 };
std::vector<std::string> result;
// v1[n] の文字を v2[n] 回繰り返した文字列を返す
std::ranges::transform(v1, v2, std::back_inserter(result), [](char a, int b) { return std::string(b, a); });
for (const std::string& s : result) {
std::cout << s << std::endl;
}
}
出力
aaa
b
cccc
並列アルゴリズムの例 (C++26)
#include <algorithm>
#include <iostream>
#include <vector>
#include <execution>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> result(v.size());
// 並列に全ての要素を2乗する
std::ranges::transform(std::execution::par, v, result, [](int x) { return x * x; });
for (int x : result) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
出力
1 4 9 16 25
バージョン
言語
- C++20
処理系
- Clang: ??
- GCC: 10.1.0 ✅
- ICC: ??
- Visual C++: 2019 Update 10 ✅
参照
関連項目
transform_view: 要素に関数を適用した結果をビューとして提供するzip_transform_view: 複数の範囲から要素を取り出し、関数を適用した結果をビューとして提供する