std::ranges:: transform, std::ranges:: unary_transform_result, std::ranges:: binary_transform_result
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O,
std::
copy_constructible
F,
class
Proj
=
std::
identity
>
|
(1) | (C++20 起) |
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O,
std::
copy_constructible
F,
class
Proj
=
std::
identity
>
|
(2) | (C++20 起) |
|
template
<
std::
input_iterator
I1,
std::
sentinel_for
<
I1
>
S1,
std::
input_iterator
I2,
std::
sentinel_for
<
I2
>
S2,
|
(3) | (C++20 起) |
|
template
<
ranges::
input_range
R1,
ranges::
input_range
R2,
|
(4) | (C++20 起) |
|
辅助类型
|
||
|
template
<
class
I,
class
O
>
using unary_transform_result = ranges:: in_out_result < I, O > ; |
(5) | (C++20 起) |
|
template
<
class
I1,
class
I2,
class
O
>
using binary_transform_result =</span |
将给定函数应用于某个范围,并将结果存储在始于 result 的另一范围中。
[
first1
,
last1
)
定义的区间(在使用投影
proj
进行投影之后)。
[
first1
,
last1
)
定义,另一个由
[
first2
,
last2
)
定义(在分别通过投影函数
proj1
和
proj2
进行投影之后)。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| first1, last1 | - | 定义待转换第一个 范围 元素的迭代器-哨位对 |
| r, r1 | - | 待转换的第一个元素范围 |
| first2, last2 | - | 定义待转换第二个 范围 元素的迭代器-哨位对 |
| r2 | - | 待转换的第二个元素范围 |
| result | - | 目标范围的起始位置,可等于 first1 或 first2 |
| op, binary_op | - | 应用于投影元素的操作 |
| proj1 | - | 应用于第一个范围元素的投影 |
| proj2 | - | 应用于第二个范围元素的投影 |
返回值
unary_transform_result
包含一个等于
last
的输入迭代器,以及一个指向最后一个被转换元素之后位置的输出迭代器。
binary_transform_result
包含指向范围
[
first1
,
last1
)
和
[
first2
,
last2
)
中最后被转换元素的输入迭代器,分别作为
in1
和
in2
,以及指向最后一个被转换元素之后位置的输出迭代器作为
out
。
复杂度
可能的实现
struct transform_fn { // 第一个版本 template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, std::copy_constructible F, class Proj = std::identity> requires std::indirectly_writable<O, std::indirect_result_t<F&, std::projected<I, Proj>>> constexpr ranges::unary_transform_result<I, O> operator()(I first1, S last1, O result, F op, Proj proj = {}) const { for (; first1 != last1; ++first1, (void)++result) *result = std::invoke(op, std::invoke(proj, *first1)); return {std::move(first1), std::move(result)}; } // 第二个版本 template<ranges::input_range R, std::weakly_incrementable O, std::copy_constructible F, class Proj = std::identity> requires std::indirectly_writable<O, std::indirect_result_t<F&, std::projected<ranges::iterator_t<R>, Proj>>> constexpr ranges::unary_transform_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, F op, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(result), std::move(op), std::move(proj)); } // 第三个版本 template<std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, std::weakly_incrementable O, std::copy_constructible F, class Proj1 = std::identity, class Proj2 = std::identity> requires std::indirectly_writable<O, std::indirect_result_t<F&, std::projected<I1, Proj1>, std::projected<I2, Proj2>>> constexpr ranges::binary_transform_result<I1, I2, O> operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}) const { for (; first1 != last1 && first2 != last2; ++first1, (void)++first2, (void)++result) *result = std::invoke(binary_op, std::invoke(proj1, *first1), std::invoke(proj2, *first2)); return {std::move(first1), std::move(first2), std::move(result)}; } // 第四个版本 template<ranges::input_range R1, ranges::input_range R2, std::weakly_incrementable O, std::copy_constructible F, class Proj1 = std::identity, class Proj2 = std:: |
注释
ranges::transform
不保证按顺序应用
op
或
binary_op
。若需按顺序对序列应用函数,或应用会修改序列元素的函数,请使用
ranges::for_each
。
示例
以下代码使用
ranges::transform
通过
std::toupper
函数将字符串就地转换为大写,然后将每个
char
转换为其序数值。
接着使用带投影的
ranges::transform
将
std::
vector
<
Foo
>
的元素转换为字符以填充
std::string
。
#include <algorithm> #include <cctype> #include <functional> #include <iostream> #include <string> #include <vector> int main() { std::string s{"hello"}; auto op = [](unsigned char c) -> unsigned char { return std::toupper(c); }; namespace ranges = std::ranges; // 就地大写化字符串 ranges::transform(s.begin(), s.end(), s.begin(), op ); std::vector<std::size_t> ordinals; // 将每个 char 转换为 size_t ranges::transform(s, std::back_inserter(ordinals), [](unsigned char c) -> std::size_t { return c; }); std::cout << s << ':'; for (auto ord : ordinals) std::cout << ' ' << ord; // 将每个序数值翻倍 ranges::transform(ordinals, ordinals, ordinals.begin(), std::plus{}); std::cout << '\n'; for (auto ord : ordinals) std::cout << ord << ' '; std::cout << '\n'; struct Foo { char bar; }; const std::vector<Foo> f = {{'h'},{'e'},{'l'},{'l'},{'o'}}; std::string result; // 投影后大写化 ranges::transform(f, std::back_inserter(result), op, &Foo::bar); std::cout << result << '\n'; }
输出:
HELLO: 72 69 76 76 79 144 138 152 152 158 HELLO
参见
|
(C++20)
|
对
范围
中的元素应用一元
函数对象
(算法函数对象) |
对每个元素应用转换函数的序列
view
(类模板) (范围适配器对象) |
|
|
对元素范围应用函数,将结果存储到目标范围中
(函数模板) |